-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHappyNumber.java
More file actions
executable file
·39 lines (35 loc) · 887 Bytes
/
HappyNumber.java
File metadata and controls
executable file
·39 lines (35 loc) · 887 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
package code.coder.lee.easy;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Created by bcc on 16/3/23.
*/
public class HappyNumber {
public boolean isHappy(int n) {
if (n == 1){
return true;
}
Set<Integer> set = new HashSet<Integer>();
set.add(n);
while (n != 1){
int temp = n;
int count = 0;
while (temp != 0){
int mod = temp % 10;
count += mod * mod;
temp = temp/10;
}
n = count;
if (set.contains(count)){
return false;
}
set.add(count);
}
return true;
}
public static void main(String[] args) {
HappyNumber happyNumber = new HappyNumber();
System.out.println(happyNumber.isHappy(64));
}
}