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 24a9c8a

Browse files
Add Solution.go for 0038.Count and Say
1 parent 1f69e60 commit 24a9c8a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func countAndSay(n int) string {
2+
buf := bytes.NewBufferString("1")
3+
for i := 2; i <= n; i++ {
4+
s := buf.String()
5+
c, l := s[0:1], len(s)
6+
buf.Reset()
7+
count := 0
8+
for j := 0; j < l; j++ {
9+
if c == s[j:j+1] {
10+
count++
11+
} else {
12+
buf.WriteByte(byte(48 + count))
13+
buf.WriteString(c)
14+
count = 1
15+
c = s[j : j+1]
16+
}
17+
}
18+
buf.WriteByte(byte(48 + count))
19+
buf.WriteString(c)
20+
}
21+
return buf.String()
22+
}

0 commit comments

Comments
(0)

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