If I definate sum in the line 3 instead of line 13, the code fails to pass, why?


  • 0
    O

    class Solution
    {
    public:
    bool isHappy(int n) {
    int temp = 0;
    unordered_set<int> myset;
    //int sum = 0; //If I definite sum here, the code will not pass, why?
    if(n == 1){
    return true;
    }
    while (1)
    {
    int sum = 0;
    while(n != 0){
    temp = n % 10;
    n = n / 10;
    sum = sum + temp * temp;
    }
    if(sum == 1){
    return true;
    }
    else{
    if(myset.find(sum) == myset.end()){
    myset.insert(sum);
    }
    else{
    return false;
    }
    }
    n = sum;

    	}
        return true;
    

    }
    };


Log in to reply
 

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