# 3ms java solution, beat 80%

• in the range of integer, the sum of each digit will never be bigger than 900, so i used int[] which size is 1000.

we just need to get sum = 1 or sum = one of the previous sum(which means infinity loop occurred).

``````public class Solution
{
public boolean isHappy(int n)
{
int[] map = new int[1000];

while(n != 1)
{
n = getSum(n);
map[n]++;
if (map[n] > 1)
return false;
}

return true;
}

public int getSum(int num)
{
char[] temp = (num + "").toCharArray();
int sum = 0;

for (int i = 0; i < temp.length; i++)
{
sum += ((temp[i] - '0') * (temp[i] - '0'));
}

return sum;
}
}``````

• Could you please explain why every digits sum is less 900? I didn't get it well. thanks

• And , Since int has 32 digits, isn't it 32*81=2592 ? so the size should be 2592?

• because positive integer range is from 0 to 2147483648 which is 10 digits, we just assume every digit can be max number 9 which means max sum can only less than 810

• this answer has a mathematical explanation, which is really cool:
https://leetcode.com/discuss/79442/hashset-time-check-whether-the-loop-with-mathematical-proof

• Great! Thank you!

• for binary int is 32 digits, but decimal number range is 0 to 2147483648 which is 10 digits

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