-
Notifications
You must be signed in to change notification settings - Fork 0
/
202-HappyNumber.cpp
51 lines (46 loc) · 1.19 KB
/
202-HappyNumber.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
47
48
49
50
51
/*=============================================================================
# FileName: 202-HappyNumber.cpp
# Desc:
# Author: qsword
# Email: huangjian1993@gmail.com
# HomePage:
# Created: 2015-05-12 21:38:55
# Version: 0.0.1
# LastChange: 2015-05-12 21:38:55
# History:
# 0.0.1 | qsword | init
=============================================================================*/
#include <leetcode.h>
#include <unordered_set>
class Solution {
public:
//11ms
bool isHappy(int n) {
if (n == 1) {
return true;
}
unordered_set<int> nums;
stringstream ss;
while (true) {
if (nums.find(n) != nums.end()) {
return false;
}
nums.insert(n);
string str;
ss.clear();
ss << n;
ss >> str;
n = 0;
for (int i = 0; i < str.length(); i ++) {
n += (str[i] - '0') * (str[i] - '0');
}
if (n == 1) {
return true;
}
}
}
};
int main() {
Solution solution;
printf("%d\n", solution.isHappy(7));
}