-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_202.java
More file actions
35 lines (28 loc) · 703 Bytes
/
Copy pathP_202.java
File metadata and controls
35 lines (28 loc) · 703 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package leetcode.easy;
public final class P_202 {
public static boolean isHappy(int n) {
int slow = n, fast = n;
do {
slow = f(slow);
fast = f(f(fast));
if (slow == 1 || fast == 1) {
return true;
}
} while (slow != fast);
return false;
}
private static int f(int n) {
int res = 0;
while (n != 0) {
final int i = n % 10;
res += i * i;
n /= 10;
}
return res;
}
public static void main(String[] args) {
System.out.println(isHappy(19));
System.out.println(isHappy(7));
}
private P_202() {}
}