3 ms in java


  • 0
    N
    class Solution {
        public boolean isHappy(int n) {
            boolean[] hasAppeared = new boolean [811];
            for(boolean temp: hasAppeared){
                temp = false;
            }
            while(n != 1){
                int temp = 0;
                while(n != 0){
                    temp += (n % 10) * (n % 10);
                    n /= 10;
                }
                n = temp;
                if(hasAppeared[n])return false;
                hasAppeared[n] = true;
            }
            return true;
        }
    }
    

Log in to reply
 

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