Skip to content

Commit 8ae9b96

Browse files
committed
Refactored solution 412 using string concatenation
1 parent 578c85e commit 8ae9b96

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

leetcode/0412.Fizz-Buzz/412. Fizz Buzz.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ package leetcode
33
import "strconv"
44

55
func fizzBuzz(n int) []string {
6-
if n < 0 {
7-
return []string{}
8-
}
96
solution := make([]string, n)
7+
108
for i := 1; i <= n; i++ {
11-
if i%3 == 0 && i%5 == 0 {
12-
solution[i-1] = "FizzBuzz"
13-
} else if i%3 == 0 {
14-
solution[i-1] = "Fizz"
15-
} else if i%5 == 0 {
16-
solution[i-1] = "Buzz"
17-
} else {
9+
solution[i-1] = ""
10+
11+
if i%3 == 0 {
12+
solution[i-1] += "Fizz"
13+
}
14+
15+
if i%5 == 0 {
16+
solution[i-1] += "Buzz"
17+
}
18+
19+
if solution[i-1] == "" {
1820
solution[i-1] = strconv.Itoa(i)
1921
}
2022
}
23+
2124
return solution
2225
}

0 commit comments

Comments
 (0)