Java 6 ms Straightforward Solution


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

Log in to reply
 

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