Created
April 2, 2020 16:52
-
-
Save gumeniukcom/d7c1e09cd94c2ff4ea36de4a5fbaf952 to your computer and use it in GitHub Desktop.
leetcode.com 202. Happy Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func isHappy(n int) bool { | |
if n <=0 { | |
return false | |
} | |
used := make(map[int]bool) | |
used[n] = true | |
for n!=1 { | |
fmt.Println(n) | |
k:= n | |
sum := 0 | |
for k>0 { | |
w:=k%10 | |
k = k/10 | |
sum +=w*w | |
} | |
if _, ok := used[sum]; ok { | |
return false | |
} | |
used[sum] = true | |
n = sum | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment