mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
Refactored solution 412 using string concatenation
This commit is contained in:
@ -3,20 +3,23 @@ package leetcode
|
||||
import "strconv"
|
||||
|
||||
func fizzBuzz(n int) []string {
|
||||
if n < 0 {
|
||||
return []string{}
|
||||
}
|
||||
solution := make([]string, n)
|
||||
|
||||
for i := 1; i <= n; i++ {
|
||||
if i%3 == 0 && i%5 == 0 {
|
||||
solution[i-1] = "FizzBuzz"
|
||||
} else if i%3 == 0 {
|
||||
solution[i-1] = "Fizz"
|
||||
} else if i%5 == 0 {
|
||||
solution[i-1] = "Buzz"
|
||||
} else {
|
||||
solution[i-1] = ""
|
||||
|
||||
if i%3 == 0 {
|
||||
solution[i-1] += "Fizz"
|
||||
}
|
||||
|
||||
if i%5 == 0 {
|
||||
solution[i-1] += "Buzz"
|
||||
}
|
||||
|
||||
if solution[i-1] == "" {
|
||||
solution[i-1] = strconv.Itoa(i)
|
||||
}
|
||||
}
|
||||
|
||||
return solution
|
||||
}
|
||||
|
Reference in New Issue
Block a user