Simple Java Solution


  • 0
    A
    public class Solution {
        HashSet<Integer> hs = new HashSet<Integer>();
        public boolean isHappy(int n) {
            if (n == 1){
                return true;
            }
            else {
                int sum = 0;
                while (n > 0){
                    int x = n % 10;
                    sum += x * x;
                    n /= 10;
                }
                if (!hs.contains(sum)){
                    hs.add (sum);
                    return isHappy (sum);
                }
                else {
                    return false;
                }
            }
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.