Clean Java Code


  • 0
    A
    public boolean isHappy(int n) {
    	Set<Integer> set = new HashSet<Integer>();
    	do {
    		set.add(n);
    		n = getSquires(n);
    	} while (!set.contains(n) && n != 1);
    	return n == 1;
    }
    
    int getSquires(int x) {
    	if (x == 0) return 0;
    	return (x % 10) * (x % 10) + getSquires(x / 10);
    }

Log in to reply
 

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