My 2 milli second code


  • 0
    J
     public class Solution {
        public boolean isHappy(int n) {
            
            do{
                n=squareSum(n);
            }while(n>9);
        if(n==1){
            return true;
        }
        else{
        return false;
        }
    }
        public int squareSum(int n){
           int sum=0;
           while(n>0){
                int l=n%10;
                sum+=(l)*(l);
                n=n/10;
            }
            return sum;
        }
    }

  • 0
    I
    This post is deleted!

  • 0
    J

    Hi,
    But as there is a do While loop, 7 will be picked up for the first time and hence will not be excluded.


  • 0
    J

    considering n==7, the answer should be true. however, your code gave a false....


  • 0
    W

    i think if n=1111111
    so squareSum(1111111)will be 7
    so your answer will be false,
    but its a happy number .


  • 0
    W

    i think if n=1111111
    so squareSum(1111111)will be 7
    so your answer will be false,
    but its a happy number .


Log in to reply
 

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