1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class Solution { private int getNext(int n){ int sum = 0; while (n > 0){ int digit = n%10; sum += (digit*digit); n/=10; } return sum; } public boolean isHappy(int n) { if (n <= 0) return false; HashSet<Integer> lookup = new HashSet<Integer>(); while (n > 1){ if (lookup.contains(n)){ return false; } lookup.add(n); n = getNext(n); } return true; } } |
No comments:
Post a Comment