Created
August 20, 2015 15:51
-
-
Save xynophon/e94d5202bdd8763a0820 to your computer and use it in GitHub Desktop.
LeetCode Happy Number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class HappyNumber { | |
| public boolean isHappy(int n) { | |
| HashSet<Integer> sum_set = new HashSet<>(); | |
| int sum = 0; | |
| while(n != 1) { | |
| while (n > 0) { | |
| sum += Math.pow(n % 10, 2); | |
| n /= 10; | |
| } | |
| if (sum_set.add(sum) == false) { | |
| return false; | |
| } | |
| n = sum; | |
| sum = 0; | |
| } | |
| return true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment