0ms C resolution


  • 0
    H
    bool isHappy(int n) {
        //if(n==1) return true;
        int r=0;
        int digit=n;
        int tmp=n;
        while(1)
        {
            while(tmp!=0)
            {
                digit=tmp%10;
                r+=digit*digit;
                tmp=tmp/10;
            }
            if(r==4) return false;
            else if(r==1) return true;
            else 
            {
                tmp=r;
                r=0;
            }
        }
    }
    

    each !isHappy num will encounter 4 in the loop.


  • 0
    P

    Hi! Can you please explain a little bit about:

    if (r==4) return false;
    

  • 0
    P
    This post is deleted!

  • 0
    P

    once we get 4 while adding up the squares of digits, it ends up in a loop. numbers 2,3,5,6,8,9 are not happy numbers because for all these numbers, while adding up the squares of digits we get 4(then we have a loop). Hence, hui6075 is checking for 4


  • 0
    N

    you wrote "if (r==4) return false"
    that means you think for all unhappy numbers, the results will be a loop and this loop will somewhere goes to 4.
    So can you prove that?


Log in to reply
 

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