java my solution 2 ms 87%


  • 0
    P
    public class Solution {
    	public boolean isHappy(int n) {
    		int sum = n;
    		while(sum != 1) {
    			if(sum == 4 || sum == 0) return false;
    			sum = getSum(sum);
    		}
    		return true;
    	}
    
    	public static int getSum(int n) {
    		int sum = 0;
    		while(n > 0) {
    			sum += (n%10)*(n%10);
    			n /= 10;
    		}
    		return sum;
    	}
    }

Log in to reply
 

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