Why does my code work for 100 but not for 68?


  • 0
    P
    class Solution {
    public:
        bool isHappy(int n) {
            int multiply = 0;
            int temp = 0;
            int cut = n;
            while(multiply != 1){
                    temp = cut % 10;
    
                    multiply = (temp*temp) + multiply;
                    cut = cut/10;
                    if(cut == 0) cut = multiply;
            }
            if (multiply == 1) return true;
        }
    };
    

    Any help appreciated. Not sure why it won't work if you pass in 68..


  • 1
    P

    There is a loop when the number is not a happy number and your solution will never get out the loop
    Your solution will never return false...


  • 0
    P

    it says to keep looping if it isn't a happy number...68 is a happy number since it becomes 100..


Log in to reply
 

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