Skip to content

Commit 11700e3

Browse files
committed
happy_number
1 parent 8a33730 commit 11700e3

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
160160
#### [199. Binary Tree Right Side View](https://github.com/hitzzc/go-leetcode/tree/master/binary_tree_right_side_view)
161161
#### [200. Number of Islands](https://github.com/hitzzc/go-leetcode/tree/master/number_of_islands)
162162
#### [201. Bitwise AND of Numbers Range](https://github.com/hitzzc/go-leetcode/tree/master/bitwise_AND_of_numbers_range)
163+
#### [202. Happy Number](https://github.com/hitzzc/go-leetcode/tree/master/happy_number)
163164

164165

165166

happy_number/happy_number.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package happy_number
2+
3+
func isHappy(n int) bool {
4+
happened := map[int]bool{}
5+
for n != 1 {
6+
n = squares(n)
7+
if happened[n] {
8+
return false
9+
}
10+
happened[n] = true
11+
}
12+
return true
13+
}
14+
15+
func squares(n int) (ret int) {
16+
for n > 0 {
17+
num := n % 10
18+
ret += num * num
19+
n /= 10
20+
}
21+
return
22+
}

0 commit comments

Comments
 (0)