Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. class Solution {
  2. // 202. Happy Number
  3.  
  4. public boolean isHappy(int n) {
  5. Set<Integer> set = new HashSet<>();
  6.  
  7. while (!set.contains(n)) {
  8. set.add(n);
  9. n = getSum(n);
  10.  
  11. if (n == 1)
  12. return true;
  13. }
  14. return false;
  15. }
  16.  
  17. private int getSum(int n) {
  18. int sum = 0;
  19.  
  20. while (n > 0) {
  21. sum += (n % 10) * (n % 10);
  22. n = n / 10;
  23. }
  24. return sum;
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement