My Java Solution


  • 0
    N
    public class Solution {
        private Set<Integer> set = new HashSet<Integer>() {{ add(0); }};
        
        public boolean isHappy(int n) {
            if (n == 1) {
                return true;
            }
            if (!set.add(n)) {
                return false;
            }
            int newVal = 0;
            while (n != 0) {
                newVal += Math.pow((n % 10), 2);
                n /= 10;
            }
            return isHappy(newVal);
        }
    }

Log in to reply
 

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