1ms java solution beats 90%


  • 1
    D

    unhappy number 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4

        public boolean isHappy(int n) {
            if(n==1){
                return true;
            }
            if(n==4){
                return false;
            }
            int sum =0;
            while (n>0){
                sum+=(n%10)*(n%10);
                n = n/10;
            }
            return isHappy(sum);
        }
    

  • 0
    S

    Programmers just can't beat mathematicians.


Log in to reply
 

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