-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path202.cpp
More file actions
35 lines (32 loc) · 677 Bytes
/
202.cpp
File metadata and controls
35 lines (32 loc) · 677 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
//
// 202.cpp
// LeetCode
//
// Created by 张佐玮 on 15/5/17.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Title: Happy Number
//
#include <iostream>
#include <unordered_set>
using namespace std;
class Solution {
public:
bool isHappy(int n) {
unordered_set<int> pastNum;
int sum;
do {
sum = 0;
while (n) {
sum += (n % 10) * (n % 10);
n /= 10;
}
if (pastNum.find(sum) != pastNum.end()) {
return false;
}
n = sum;
pastNum.insert(n);
} while (n != 1);
return true;
}
};