forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0202.cpp
More file actions
32 lines (32 loc) · 694 Bytes
/
0202.cpp
File metadata and controls
32 lines (32 loc) · 694 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
#include <iostream>
#include <unordered_set>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
bool isHappy(int n)
{
unordered_set<int> sum_set;
while (n != 1)
{
int num_sum = 0;
while (n > 0)
{
int k = n % 10;
num_sum += k*k;
n /= 10;
}
if (sum_set.count(num_sum) >= 1) return false;
sum_set.insert(num_sum);
n = num_sum;
}
return true;
}
};
int main()
{
int n = 19;
cout << Solution().isHappy(n) << endl;
return 0;
}