2 ms Java Solution without MOD (%)


  • 0
    W
    public static boolean isHappy(int n) {
    int sum = 2, old;
    final boolean[] map = new boolean[730];
    while (sum > 1) {
        if (map[sum]) return false;
        map[sum] = true;
        sum = 0;
        while (n > 0) {
            old = n;
            n /= 10;
            old -= n * 10;
            sum += old * old;
        }
        n = sum;
    }
    return sum == 1;
    

    }

    Why 730?
    We got integer value input, then max possibly point is (9^2+9^2+9^2+9^2+9^2+9^2+9^2+9^2+9^2) 729.


  • 0
    K

    0-729 = 730 total options


  • 0
    W

    Your true ;) but anyway it's looks fast.


Log in to reply
 

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