Happy Number

Posted on

Screen Shot 2015-10-08 at 11.00.40 AM

Idea:

Use a HashSet to store each square sum.

  • boolean add() method in HashSet: repeated new element cannot be inserted
  • If the new add-in sum repeats, jump out the while loop and return false.
  • In each iteration, replace n with new sum and reset sum each time at the start of iteration
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s