add new solutions
This commit is contained in:
parent
59b71480d4
commit
71189b61cf
8 changed files with 200 additions and 0 deletions
29
solutions/2/q202/solution.go
Normal file
29
solutions/2/q202/solution.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package q202
|
||||
|
||||
type void struct{}
|
||||
|
||||
func next(n int) int {
|
||||
ret := 0
|
||||
for n > 0 {
|
||||
digit := n % 10
|
||||
n /= 10
|
||||
ret += digit * digit
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func isHappy(n int) bool {
|
||||
seen := map[int]void{n: {}}
|
||||
for {
|
||||
n = next(n)
|
||||
if n == 1 {
|
||||
return true
|
||||
}
|
||||
if _, ok := seen[n]; ok {
|
||||
return false
|
||||
}
|
||||
seen[n] = void{}
|
||||
}
|
||||
}
|
||||
|
||||
var _ = isHappy
|
||||
Loading…
Add table
Add a link
Reference in a new issue