Simple c++ solution


  • 0
    K
    class Solution {
    public:
        bool isHappy(int n) {
            int k, digit;
            int i = 0;
            while(n>1){
                k = 0;
                while(n){
                     digit = n%10;
                    k += digit*digit;
                    n/=10;
                }
                if(i++ > 8) break;
                n=k;
            }
            return n == 1;
        }
    };

  • 0
    S

    why 8 for breaking the loop?
    does it work for big numbers, like 999999999?


Log in to reply
 

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