Share my easy java solution


  • 0
    A

    Maintain a list to record the every new number. If there is any same number, that means there is a loop.

    class Solution {
        public boolean isHappy(int n) {
            List<Integer> record = new ArrayList<Integer>();
            int num = n;
            while(true){
    
                int temp = 0;
                int r = 0;
                while(num>0){
                    r = num%10;
                    temp = temp + r*r;
                    num = num/10;
                }
                
                if(temp==1)
                    return true;
                if(record.contains(temp)){
                    return false;
                }else{
                    record.add(temp);
                }
                num = temp;
            }
        }
    }
    

Log in to reply
 

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