-
Notifications
You must be signed in to change notification settings - Fork 95
/
happy-number.cpp
46 lines (40 loc) · 993 Bytes
/
happy-number.cpp
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
//time: O(1)
//space: O(1) <- O(891)
class Solution {
private:
//0: uninitialized
//1: happy num
//2: not happy num
//3: being set
int answer[9*9*11];
public:
Solution() {
memset(answer, 0, sizeof(answer));
for (int i = 1; i < 9 * 9 * 11; ++i) {
judgeHappy(i);
}
}
int judgeHappy(int num) {
if (answer[num] == 1 || answer[num] == 2) {
return answer[num];
} else if (answer[num] == 3) {
return 2;
} else if (num == 1) {
return answer[num] = 1;
}
answer[num] = 3;
return answer[num] = judgeHappy(digitSquareSum(num));
}
int digitSquareSum(int num) {
int res = 0, digit = 0;
while (num != 0) {
digit = num % 10;
res += digit * digit;
num /= 10;
}
return res;
}
bool isHappy(int n) {
return answer[digitSquareSum(n)] == 1 ? true : false;
}
};