My Java solution


  • 0
    R

    public class Solution {

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

    }


Log in to reply
 

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