Java Solution with 2ms, beat 85%


  • 1
    public class Solution {
    public boolean isHappy(int n) {
        if(n==1) return true;
        int[] array =  new int[810];
        if(n<810) array[n]++;
        int sum;
        while(n!=1){
            sum=0;
            while(n>0){
                sum+=(n%10)*(n%10);
                n=n/10;
            }
            array[sum]++;
            if(array[sum]>1) return false;
            n=sum;
        }
       return true;
    }
    

    }


  • 0
    E

    Could you explain what does 810 mean, please? Thank you.


  • 0

    int value is less than 2147483647, it is 10 length, so the power of the each digit is less than 9^2*10=810.


  • 0
    J

    What does array mean? Thank you!


  • 0

    it saves all the possible value after the power of each digit in the number..


Log in to reply
 

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