Skip to content

Commit 28f199e

Browse files
committed
Time: 3 ms (18.49%), Space: 9.3 MB (5.06%) - LeetHub
1 parent ddf8e05 commit 28f199e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
bool isHappy(int n) {
4+
map<int, bool> seen;
5+
while(n != 1) {
6+
cout << n << ' ';
7+
if (seen[n]) return false;
8+
seen[n] = true;
9+
n = digsum(n);
10+
}
11+
return true;
12+
}
13+
14+
int digsum(int n) {
15+
int sum = 0;
16+
while(n) {
17+
sum += (n % 10) * (n % 10);
18+
n /= 10;
19+
}
20+
return sum;
21+
}
22+
};

0 commit comments

Comments
 (0)