-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHappyNumber.java
More file actions
49 lines (41 loc) · 1012 Bytes
/
HappyNumber.java
File metadata and controls
49 lines (41 loc) · 1012 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<<<<<<< HEAD
//202. Happy Number
import java.util.HashSet;
import java.util.Set;
class Solution {
public boolean isHappy(int n) {
Set<Integer> seen = new HashSet<>();
while (n != 1 && !seen.contains(n)) {
seen.add(n);
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += (digit * digit);
n /= 10;
}
n = sum;
}
return n == 1;
}
}
=======
//202. Happy Number
import java.util.HashSet;
import java.util.Set;
class Solution {
public boolean isHappy(int n) {
Set<Integer> seen = new HashSet<>();
while (n != 1 && !seen.contains(n)) {
seen.add(n);
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += (digit * digit);
n /= 10;
}
n = sum;
}
return n == 1;
}
}
>>>>>>> be6ce0b427078b1421d5dd74adb2300dc02daeec