-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHappyNumber_202.java
61 lines (55 loc) · 1.45 KB
/
HappyNumber_202.java
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
50
51
52
53
54
55
56
57
58
59
60
61
/**
* - Use a HashSet to record whether a number has already exists
* If yes, then it forms a cycle, return false
* - Input is a positive number
* - O(n) extra space, were n is the size of the cycle
*/
public class Solution {
public boolean isHappy(int n) {
Set<Integer> set = new HashSet<>();
set.add(n);
while(true) {
if (n == 1)
return true;
int sum = 0;
while(n>0) {
sum += (n%10) * (n%10);
n = n / 10;
}
if(set.contains(sum))
return false;
else {
set.add(sum);
n = sum;
}
}
}
}
/**
* How to solve this without using extra spaces
* - Remeber how we test whether a linked list has a cycle : Fast & Slow Pointers
* - If there is a cycle, fast will catch the slow pointer finally
* - Floyd Cycle-Finding Algorithm
* - k + t = n*c => k = nc-t
*/
public class Solution {
public boolean isHappy(int n) {
int slow = n, fast = ssum(n);
while(true) {
if (fast == 1)
return true;
else if (slow == fast)
return false;
fast = ssum(ssum(fast));
slow = ssum(slow);
}
}
private int ssum(int n) {
int sum = 0;
while(n > 0) {
sum += (n%10) * (n%10);
n /= 10;
}
return sum;
}
}