Java solution 2ms


  • 0
    T
    public class Solution {
    public boolean isHappy(int n) {
        boolean[] exist = new boolean[1000];
        while(true){
            if(n == 1) return true;
            int m = 0;
            while(n > 0){
                m += (n % 10) * (n % 10);
                n /= 10;
            }
            n = m;
            if(exist[m] == true) return false;
            exist[m] = true;
        }
    }
    

    }


  • 0
    W

    What if n is larger than 1000?


  • 0
    T

    @WTCCTW the size of array is enough for this solution,.if n = 99999999;
    this moment m = 648b < 1000; the value of m decide the index of array


Log in to reply
 

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