Run Time 0 ms, written in C


  • 3
    A
    bool isHappy(int n) {
        int n_copy = n;
        while (n_copy >= 10 ) {
            n_copy = IsHappySingleStep(n_copy);
        }
        return n_copy == 1 || n_copy == 7;
    }
    
    int IsHappySingleStep(int n) {
        int result = 0;
        int lastNum = n % 10;
        while (n != 0){
            result += lastNum * lastNum;
            n /= 10;
            lastNum = n % 10;
        }
        return result;
    }

  • 0
    X

    Is that possible to prove any position integer could be reduced to a single digit?


Log in to reply
 

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