Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0e034c0

Browse files
solution of happy number problem
1 parent fcc3843 commit 0e034c0

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎leetcode/happy_number-solution-1.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// https://leetcode.com/problems/happy-number/
2+
package main
3+
4+
func isHappy(n int) bool {
5+
nums := make(map[int]struct{})
6+
for {
7+
nn := calc(n)
8+
if nn == 1 {
9+
return true
10+
}
11+
if nn <= 0 {
12+
return false
13+
}
14+
if _, ok := nums[nn]; ok {
15+
return false
16+
}
17+
nums[nn] = struct{}{}
18+
n = nn
19+
}
20+
return false
21+
}
22+
23+
func calc(n int) int {
24+
r := int(0)
25+
for ; n > 0; n /= 10 {
26+
c := n % 10
27+
r = r + c*c
28+
}
29+
return r
30+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /