Hamlet and Ophelia, who before the play had been "dating" if you will, had a pretty close relationship. Hamlet had been wooing her, and giving her love letters and presents; Ophelia's brother and father were highly suspicious of his intentions. In opening scenes of the play, they both separately warn her against Hamlet, insisting that all he wants to do is get some action from her, not marry her or be honorable whatsoever. Ophelia doesn't necessarily agree, but, she is a weak-willed character, and agrees to break things off with him. Before this resolution even occurs though, she describes a very strange encounter that she had with Hamlet.
In Ophelia's description she said that Hamlet came up to her dressed very strangely, with his stockings askew, his shirt untucked, and no hat, and that he was
"pale as his shirt; his knees knocking each other; and with a look so piteous in purport as if he had been loosed out of hell."
So, he's all rumpled and flustered, super pale, and looks like he's just seen a ghost or been super upset by something. He then grabbed her hand, and with his other hand, felt her face "as he would draw it." So he's standing there, acting strange, feeling her face like he wants to memorize it, and then, he
"raised a sigh so piteous and profound as it did seem to shatter all his bulk."
He shook her arm a few times, shook his head a few times, then left the room, all the while keeping his eyes fastened on her.
This is very strange behavior, that is pretty much left unexplained in the play. We have to guess why he was acting so weird, and infer his motivations, based on the clues from the play. He is really upset at his mother marrying his uncle soo quickly after his father's death. Perhaps it caused him to lose all faith in all women, and in their affections. Perhaps he felt that Ophelia too would someday betray him, and so, even though he loved her, he decided to end it first. But, before that very difficult act, maybe he wanted one last glimpse of her to keep in his memory. His behavior is enough to startle Ophelia senseless, and prompt Polonius to spy on Hamlet to figure out what's going on. I hope that those thoughts helped; good luck!