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 @@ -129,21 +129,21 @@ public:
129
129
#### Go
130
130
131
131
```go
132
- func fizzBuzz(n int) (ans []string) {
133
- for i := 1; i <= n; i++ {
134
- s := &strings.Builder{}
135
- if i%3 == 0 {
136
- s.WriteString("Fizz")
132
+ func fizzBuzz(n int) []string {
133
+ ans := make([]string, 0, n)
134
+ for i := 1; i < n+1; i++ {
135
+ switch {
136
+ case i%15 == 0:
137
+ ans = append(ans, "FizzBuzz")
138
+ case i%3 == 0:
139
+ ans = append(ans, "Fizz")
140
+ case i%5 == 0:
141
+ ans = append(ans, "Buzz")
142
+ default:
143
+ ans = append(ans, strconv.Itoa(i))
137
144
}
138
- if i%5 == 0 {
139
- s.WriteString("Buzz")
140
- }
141
- if s.Len() == 0 {
142
- s.WriteString(strconv.Itoa(i))
143
- }
144
- ans = append(ans, s.String())
145
145
}
146
- return
146
+ return ans
147
147
}
148
148
```
149
149
You can’t perform that action at this time.
0 commit comments