File tree Expand file tree Collapse file tree 1 file changed +13
-13
lines changed
solution/0400-0499/0412.Fizz Buzz Expand file tree Collapse file tree 1 file changed +13
-13
lines changed Original file line number Diff line number Diff line change @@ -141,21 +141,21 @@ public:
141141#### Go
142142
143143```go
144- func fizzBuzz(n int) (ans []string) {
145- for i := 1; i <= n; i++ {
146- s := &strings.Builder{}
147- if i%3 == 0 {
148- s.WriteString("Fizz")
144+ func fizzBuzz(n int) []string {
145+ ans := make([]string, 0, n)
146+ for i := 1; i < n+1; i++ {
147+ switch {
148+ case i%15 == 0:
149+ ans = append(ans, "FizzBuzz")
150+ case i%3 == 0:
151+ ans = append(ans, "Fizz")
152+ case i%5 == 0:
153+ ans = append(ans, "Buzz")
154+ default:
155+ ans = append(ans, strconv.Itoa(i))
149156 }
150- if i%5 == 0 {
151- s.WriteString("Buzz")
152- }
153- if s.Len() == 0 {
154- s.WriteString(strconv.Itoa(i))
155- }
156- ans = append(ans, s.String())
157157 }
158- return
158+ return ans
159159}
160160```
161161
You can’t perform that action at this time.
0 commit comments