Skip to content

Commit ac2fcc4

Browse files
Merge pull request #19 from lxmn22nov/lxmn8
Java: Hash Table
2 parents a9abe9c + c6f130e commit ac2fcc4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Java/HashTable/HappyNumber.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* PROBLEM: 202 : Happy Number
3+
*/
4+
class HappyNumber {
5+
public boolean isHappy(int n) {
6+
int slow = n;
7+
int fast = getSquareDigitSum(n);
8+
while (fast != 1 && slow != fast) {
9+
slow = getSquareDigitSum(slow);
10+
fast = getSquareDigitSum(getSquareDigitSum(fast));
11+
}
12+
return fast == 1;
13+
}
14+
15+
private int getSquareDigitSum(int n) {
16+
int squareDigitSum = 0;
17+
while (n > 0) {
18+
int digit = n % 10;
19+
squareDigitSum += digit * digit;
20+
n /= 10;
21+
}
22+
return squareDigitSum;
23+
}
24+
}

0 commit comments

Comments
 (0)