-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.java
41 lines (36 loc) · 966 Bytes
/
Solution.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
package ds.hashmap.leetcode202;
import java.util.HashSet;
/**
* 快乐数
* LeetCode 202 https://mp.weixin.qq.com/s?__biz=MzUxNjY5NTYxNA==&mid=2247484202&idx=1&sn=f07d1166d61887007c2aa8c076a07365&scene=21#wechat_redirect
*
* @author yangyi 2021年01月11日23:55:53
*/
public class Solution {
public boolean isHappy(int n) {
HashSet<Integer> set = new HashSet<>();
while (true) {
int sum = getSum(n);
if (sum == 1) {
return true;
}
if (set.contains(sum)) {
return false;
} else {
set.add(sum);
}
n = sum;
}
}
private int getSum(int n) {
int sum = 0;
while (n > 0) {
sum += Math.pow(n % 10, 2);
n /= 10;
}
return sum;
}
public static void main(String[] args) {
System.out.println(new Solution().isHappy(19));
}
}