mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
Merge pull request #152 from brenobaptista/refactored-412
Refactored solution 412 using string concatenation
This commit is contained in:
@ -3,18 +3,16 @@ 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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user