Simple Java Solution


  • 0
    E
    public class Solution {
        public boolean isHappy(int n) {
            ArrayList<Integer>list=new ArrayList<Integer>();
            while(n!=1){
                int sum=0;
                while(n!=0){
                    sum+=(int)Math.pow((n%10),2);
                    n/=10;
                }
                if(list.contains(sum) && sum!=1){
                    return false;
                }
                else{
                    list.add(sum);
                }
                n=sum;
            }
            return true;
        }
    }
    

  • 0
    S

    @emimayank so beautiful code. I do not know why, I should rely on from high to low to take this figure, really really silly.


Log in to reply
 

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