C++,6ms,use set associative-container to solve the problem


  • 0
    B

    class Solution {
    public:
    bool isHappy(int n) {
    set<int> setNum;
    int temp;
    int sum =0;
    while(1){
    if(setNum.find(n)!=setNum.end())
    {
    if(1 == n)
    return true;
    else
    return false;
    }
    else
    setNum.insert(n);
    while(n)
    {
    temp = n%10;
    sum +=temp*temp;
    n = n/10;
    }
    n = sum;
    sum = 0;
    }

    }
    

    };


Log in to reply
 

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