Simple Java solution using HashSet


  • 0

    Here is my solution:

    public boolean isHappy(int n) {
            HashSet<Integer> numbers = new HashSet<Integer>();
            while(n!=1){
                if(!numbers.add(n)){
                    return false;
                }else{
                    int tmp = 0;
                    while(n!=0){
                        int digit = n%10;
                        tmp += digit * digit;
                        n/=10;
                    }
                    n = tmp;
                }
            }
            
            return true;
        }
    

Log in to reply
 

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