Simple and Easy C++


  • 0
    R
     bool isHappy(int n) {
        unordered_map<int,int> map;
        while(true){
            int val = getSum(n);
            if(val == 1 )
                return true;
            if(map.find(val)!=map.end())
                return false;
            map[val]++;
            n = val;
        }
    }
    
    int getSum(int n){
        int sum = 0;
        while(n){
            sum+=(n%10)*(n%10);
            n/=10;
            
        }
        return sum;
    }

Log in to reply
 

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