We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 195f042 commit 6fdc494Copy full SHA for 6fdc494
solution/0400-0499/0412.Fizz Buzz/Solution.go
@@ -1,16 +1,16 @@
1
func fizzBuzz(n int) (ans []string) {
2
- for i := 1; i <= n; i++ {
3
- s := &strings.Builder{}
4
- if i%3 == 0 {
5
- s.WriteString("Fizz")
+ ans := make([]string, 0, n)
+ for i := 1; i < n+1; i++ {
+ switch {
+ case i%15 == 0:
6
+ ans = append(ans, "FizzBuzz")
7
+ case i%3 == 0:
8
+ ans = append(ans, "Fizz")
9
+ case i%5 == 0:
10
+ ans = append(ans, "Buzz")
11
+ default:
12
+ ans = append(ans, strconv.Itoa(i))
13
}
- if i%5 == 0 {
- s.WriteString("Buzz")
- }
- if s.Len() == 0 {
- s.WriteString(strconv.Itoa(i))
- ans = append(ans, s.String())
14
15
- return
+ return ans
16
0 commit comments