412. Fizz Buzz
題目
Facebook
, Microsoft
, Apple
Given an integer n, return a string array answer (1-indexed) where:
answer[i] == "FizzBuzz" if i is divisible by 3 and 5. answer[i] == "Fizz" if i is divisible by 3. answer[i] == "Buzz" if i is divisible by 5. answer[i] == i (as a string) if none of the above conditions are true.
Example 1:
Input: n = 3 Output: ["1","2","Fizz"] Example 2:
Input: n = 5 Output: ["1","2","Fizz","4","Buzz"] Example 3:
Input: n = 15 Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
Constraints:
1 <= n <= 104
題目大意
解題思路
Big O
時間複雜 : O(n)
空間複雜 : O(n)
來源
- https://leetcode.com/problems/fizz-buzz/description/
- https://leetcode.cn/problems/fizz-buzz/description/
解答
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0412.Fizz-Buzz/main.go
package fizzbuzz
import "strconv"
// 時間複雜 O(), 空間複雜 O()
func fizzBuzz(n int) []string {
var result []string
for i := 1; i
Benchmark
goos: darwin
goarch: amd64
pkg: LeetcodeGolang/Leetcode/0412.Fizz-Buzz
cpu: Intel(R) Core(TM) i5-6400 CPU @ 2.70GHz
BenchmarkFizzBuzz-4 5918809 287.1 ns/op 112 B/op 3 allocs/op
BenchmarkFizzBuzz2-4 5024536 223.8 ns/op 112 B/op 3 allocs/op
BenchmarkFizzBuzz3-4 5406643 196.3 ns/op 112 B/op 3 allocs/op
PASS
ok LeetcodeGolang/Leetcode/0412.Fizz-Buzz 5.507s