Movatterモバイル変換


[0]ホーム

URL:


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

Commit748fb79

Browse files
Merge pull requestneetcode-gh#288 from Dhiwakar92/main
Go solution for Leetcode 202 - Happy Number
2 parents3e570a9 +e6e2a48 commit748fb79

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

‎go/202-Happy-Number.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
funcisHappy(nint)bool {
2+
3+
total:=0
4+
alreadySeen:=make(map[int]bool)
5+
for {
6+
7+
// if we have seen this number
8+
ifseen:=alreadySeen[n];seen {
9+
break
10+
}
11+
12+
alreadySeen[n]=true
13+
14+
// split n into its individual digits.
15+
strn:=fmt.Sprint(n)
16+
17+
// get the length
18+
length:=len(strn)
19+
20+
// compute sum of digits
21+
fori:=0;i<length;i++ {
22+
digit,_:=strconv.Atoi(string(strn[i]))
23+
total+= (digit*digit)
24+
}
25+
iftotal==1 {
26+
returntrue
27+
}
28+
29+
// reassign n to total
30+
n=total
31+
total=0
32+
}
33+
34+
returnfalse
35+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp