Skip to content

Commit aba618a

Browse files
committed
Add Solution2.java for 0202.Happy Number
1 parent 8c22053 commit aba618a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
// fast slow point
3+
public boolean isHappy(int n) {
4+
int fast = n;
5+
int slow = n;
6+
do {
7+
slow = squareSum(slow);
8+
fast = squareSum(fast);
9+
fast = squareSum(fast);
10+
} while (slow != fast);
11+
return fast == 1 ? true : false;
12+
}
13+
14+
private int squareSum(int m) {
15+
int squaresum = 0;
16+
while (m != 0) {
17+
squaresum += (m%10) * (m%10);
18+
m /= 10;
19+
}
20+
return squaresum;
21+
}
22+
}

0 commit comments

Comments
 (0)