mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
规范格式
This commit is contained in:
22
leetcode/0412.Fizz-Buzz/412. Fizz Buzz.go
Normal file
22
leetcode/0412.Fizz-Buzz/412. Fizz Buzz.go
Normal file
@ -0,0 +1,22 @@
|
||||
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] = strconv.Itoa(i)
|
||||
}
|
||||
}
|
||||
return solution
|
||||
}
|
49
leetcode/0412.Fizz-Buzz/412. Fizz Buzz_test.go
Normal file
49
leetcode/0412.Fizz-Buzz/412. Fizz Buzz_test.go
Normal file
@ -0,0 +1,49 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var tcs = []struct {
|
||||
n int
|
||||
ans []string
|
||||
}{
|
||||
|
||||
{
|
||||
15,
|
||||
[]string{
|
||||
"1",
|
||||
"2",
|
||||
"Fizz",
|
||||
"4",
|
||||
"Buzz",
|
||||
"Fizz",
|
||||
"7",
|
||||
"8",
|
||||
"Fizz",
|
||||
"Buzz",
|
||||
"11",
|
||||
"Fizz",
|
||||
"13",
|
||||
"14",
|
||||
"FizzBuzz",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func Test_fizzBuzz(t *testing.T) {
|
||||
fmt.Printf("------------------------Leetcode Problem 412------------------------\n")
|
||||
for _, tc := range tcs {
|
||||
fmt.Printf("【output】:%v\n", tc)
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
|
||||
func Benchmark_fizzBuzz(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, tc := range tcs {
|
||||
fizzBuzz(tc.n)
|
||||
}
|
||||
}
|
||||
}
|
41
leetcode/0412.Fizz-Buzz/README.md
Normal file
41
leetcode/0412.Fizz-Buzz/README.md
Normal file
@ -0,0 +1,41 @@
|
||||
# [412. Fizz Buzz](https://leetcode.com/problems/fizz-buzz/)
|
||||
|
||||
## 题目
|
||||
|
||||
Write a program that outputs the string representation of numbers from 1 to n.
|
||||
|
||||
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
|
||||
|
||||
Example:
|
||||
|
||||
```text
|
||||
n = 15,
|
||||
|
||||
Return:
|
||||
[
|
||||
"1",
|
||||
"2",
|
||||
"Fizz",
|
||||
"4",
|
||||
"Buzz",
|
||||
"Fizz",
|
||||
"7",
|
||||
"8",
|
||||
"Fizz",
|
||||
"Buzz",
|
||||
"11",
|
||||
"Fizz",
|
||||
"13",
|
||||
"14",
|
||||
"FizzBuzz"
|
||||
]
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
3的倍数输出 "Fizz",5的倍数输出 "Buzz",15的倍数输出 "FizzBuzz",其他时候都输出原本的数字。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
按照题意做即可。
|
Reference in New Issue
Block a user