Happy Number


  • 0
    1
    class Solution {
        public boolean isHappy(int n) {
            if(n<=0)  return false;
            int count=0;
            int x;
            while(count<30)           //急中生智,防止无限循环
            {
              x=ret(n);
             if(x==1)   return true;
                count++;
                n=x;
            }
    
           return false;
        }
        public int ret(int n)
        {
            int ret=0;
             while(n>0)
            {
                
               ret+=(n%10)*(n%10);
                n/=10;
                
            }
            return ret;
        }
    }
    

  • 0
    H

    急中生智哈哈哈哈哈哈哈蛤


Log in to reply
 

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