Skip to content

Commit 721e50f

Browse files
committed
feat: Fizz-Buzz
1 parent 78be39d commit 721e50f

File tree

4 files changed

+320
-1
lines changed

4 files changed

+320
-1
lines changed

Leetcode/0412.Fizz-Buzz/README.md

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
---
2+
title: 412. Fizz Buzz
3+
subtitle: "https://leetcode.com/problems/fizz-buzz/description/"
4+
date: 2024-02-14T19:37:00+08:00
5+
lastmod: 2024-02-14T19:37:00+08:00
6+
draft: false
7+
author: "Kimi.Tsai"
8+
authorLink: "https://kimi0230.github.io/"
9+
description: "0412.Fizz-Buzz"
10+
license: ""
11+
images: []
12+
13+
tags: [LeetCode, Go, Easy, Fizz Buzz, Facebook, Microsoft, Apple, string, math]
14+
categories: [LeetCode]
15+
16+
featuredImage: ""
17+
featuredImagePreview: ""
18+
19+
hiddenFromHomePage: false
20+
hiddenFromSearch: false
21+
twemoji: false
22+
lightgallery: true
23+
ruby: true
24+
fraction: true
25+
fontawesome: true
26+
linkToMarkdown: false
27+
rssFullText: false
28+
29+
toc:
30+
enable: true
31+
auto: true
32+
code:
33+
copy: true
34+
maxShownLines: 200
35+
math:
36+
enable: false
37+
# ...
38+
mapbox:
39+
# ...
40+
share:
41+
enable: true
42+
# ...
43+
comment:
44+
enable: true
45+
# ...
46+
library:
47+
css:
48+
# someCSS = "some.css"
49+
# located in "assets/"
50+
# Or
51+
# someCSS = "https://cdn.example.com/some.css"
52+
js:
53+
# someJS = "some.js"
54+
# located in "assets/"
55+
# Or
56+
# someJS = "https://cdn.example.com/some.js"
57+
seo:
58+
images: []
59+
# ...
60+
---
61+
# [412. Fizz Buzz](https://leetcode.com/problems/fizz-buzz/description/)
62+
63+
## 題目
64+
`Facebook`, `Microsoft`, `Apple`
65+
Given an integer n, return a string array answer (1-indexed) where:
66+
67+
answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
68+
answer[i] == "Fizz" if i is divisible by 3.
69+
answer[i] == "Buzz" if i is divisible by 5.
70+
answer[i] == i (as a string) if none of the above conditions are true.
71+
72+
73+
Example 1:
74+
75+
Input: n = 3
76+
Output: ["1","2","Fizz"]
77+
Example 2:
78+
79+
Input: n = 5
80+
Output: ["1","2","Fizz","4","Buzz"]
81+
Example 3:
82+
83+
Input: n = 15
84+
Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
85+
86+
87+
Constraints:
88+
89+
1 <= n <= 104
90+
91+
## 題目大意
92+
93+
94+
## 解題思路
95+
96+
## Big O
97+
時間複雜 : ``
98+
空間複雜 : ``
99+
100+
## 來源
101+
* https://leetcode.com/problems/fizz-buzz/description/
102+
* https://leetcode.cn/problems/fizz-buzz/description/
103+
104+
## 解答
105+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0412.Fizz-Buzz/main.go
106+
107+
```go
108+
package fizzbuzz
109+
110+
import "strconv"
111+
112+
// 時間複雜 O(), 空間複雜 O()
113+
func fizzBuzz(n int) []string {
114+
var result []string
115+
for i := 1; i <= n; i++ {
116+
if i%15 == 0 {
117+
result = append(result, "FizzBuzz")
118+
} else if i%3 == 0 {
119+
result = append(result, "Fizz")
120+
} else if i%5 == 0 {
121+
result = append(result, "Buzz")
122+
} else {
123+
result = append(result, strconv.Itoa(i))
124+
}
125+
}
126+
return result
127+
}
128+
129+
func fizzBuzz2(n int) []string {
130+
var result []string
131+
for i := 1; i <= n; i++ {
132+
var str string
133+
if i%3 == 0 {
134+
str += "Fizz"
135+
}
136+
if i%5 == 0 {
137+
str += "Buzz"
138+
}
139+
if len(str) <= 0 {
140+
str = strconv.Itoa(i)
141+
}
142+
result = append(result, str)
143+
}
144+
return result
145+
}
146+
147+
// 最佳
148+
func fizzBuzz3(n int) []string {
149+
var result []string
150+
fizz := 0
151+
buzz := 0
152+
for i := 1; i <= n; i++ {
153+
fizz++
154+
buzz++
155+
if fizz == 3 && buzz == 5 {
156+
result = append(result, "FizzBuzz")
157+
fizz = 0
158+
buzz = 0
159+
} else if fizz == 3 {
160+
result = append(result, "Fizz")
161+
fizz = 0
162+
} else if buzz == 5 {
163+
result = append(result, "Buzz")
164+
buzz = 0
165+
} else {
166+
result = append(result, strconv.Itoa(i))
167+
}
168+
}
169+
return result
170+
}
171+
172+
```
173+
174+
## Benchmark
175+
176+
```sh
177+
goos: darwin
178+
goarch: amd64
179+
pkg: LeetcodeGolang/Leetcode/0412.Fizz-Buzz
180+
cpu: Intel(R) Core(TM) i5-6400 CPU @ 2.70GHz
181+
BenchmarkFizzBuzz-4 5918809 287.1 ns/op 112 B/op 3 allocs/op
182+
BenchmarkFizzBuzz2-4 5024536 223.8 ns/op 112 B/op 3 allocs/op
183+
BenchmarkFizzBuzz3-4 5406643 196.3 ns/op 112 B/op 3 allocs/op
184+
PASS
185+
ok LeetcodeGolang/Leetcode/0412.Fizz-Buzz 5.507s
186+
```

Leetcode/0412.Fizz-Buzz/main.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package fizzbuzz
2+
3+
import "strconv"
4+
5+
// 時間複雜 O(), 空間複雜 O()
6+
func fizzBuzz(n int) []string {
7+
var result []string
8+
for i := 1; i <= n; i++ {
9+
if i%15 == 0 {
10+
result = append(result, "FizzBuzz")
11+
} else if i%3 == 0 {
12+
result = append(result, "Fizz")
13+
} else if i%5 == 0 {
14+
result = append(result, "Buzz")
15+
} else {
16+
result = append(result, strconv.Itoa(i))
17+
}
18+
}
19+
return result
20+
}
21+
22+
func fizzBuzz2(n int) []string {
23+
var result []string
24+
for i := 1; i <= n; i++ {
25+
var str string
26+
if i%3 == 0 {
27+
str += "Fizz"
28+
}
29+
if i%5 == 0 {
30+
str += "Buzz"
31+
}
32+
if len(str) <= 0 {
33+
str = strconv.Itoa(i)
34+
}
35+
result = append(result, str)
36+
}
37+
return result
38+
}
39+
40+
// 最佳
41+
func fizzBuzz3(n int) []string {
42+
var result []string
43+
fizz := 0
44+
buzz := 0
45+
for i := 1; i <= n; i++ {
46+
fizz++
47+
buzz++
48+
if fizz == 3 && buzz == 5 {
49+
result = append(result, "FizzBuzz")
50+
fizz = 0
51+
buzz = 0
52+
} else if fizz == 3 {
53+
result = append(result, "Fizz")
54+
fizz = 0
55+
} else if buzz == 5 {
56+
result = append(result, "Buzz")
57+
buzz = 0
58+
} else {
59+
result = append(result, strconv.Itoa(i))
60+
}
61+
}
62+
return result
63+
}

Leetcode/0412.Fizz-Buzz/main_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package fizzbuzz
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
var tests = []struct {
9+
arg1 int
10+
want []string
11+
}{
12+
{
13+
3,
14+
[]string{"1", "2", "Fizz"},
15+
},
16+
}
17+
18+
func TestFizzBuzz(t *testing.T) {
19+
for _, tt := range tests {
20+
if got := fizzBuzz(tt.arg1); !reflect.DeepEqual(got, tt.want) {
21+
// if got := fizzBuzz(tt.arg1); got != tt.want {
22+
t.Errorf("got = %v, want = %v", got, tt.want)
23+
}
24+
}
25+
}
26+
27+
func BenchmarkFizzBuzz(b *testing.B) {
28+
b.ResetTimer()
29+
for i := 0; i < b.N; i++ {
30+
fizzBuzz(tests[0].arg1)
31+
}
32+
}
33+
34+
func TestFizzBuzz2(t *testing.T) {
35+
for _, tt := range tests {
36+
if got := fizzBuzz2(tt.arg1); !reflect.DeepEqual(got, tt.want) {
37+
// if got := fizzBuzz2(tt.arg1); got != tt.want {
38+
t.Errorf("got = %v, want = %v", got, tt.want)
39+
}
40+
}
41+
}
42+
43+
func BenchmarkFizzBuzz2(b *testing.B) {
44+
b.ResetTimer()
45+
for i := 0; i < b.N; i++ {
46+
fizzBuzz2(tests[0].arg1)
47+
}
48+
}
49+
50+
func TestFizzBuzz3(t *testing.T) {
51+
for _, tt := range tests {
52+
if got := fizzBuzz3(tt.arg1); !reflect.DeepEqual(got, tt.want) {
53+
// if got := fizzBuzz3(tt.arg1); got != tt.want {
54+
t.Errorf("got = %v, want = %v", got, tt.want)
55+
}
56+
}
57+
}
58+
59+
func BenchmarkFizzBuzz3(b *testing.B) {
60+
b.ResetTimer()
61+
for i := 0; i < b.N; i++ {
62+
fizzBuzz3(tests[0].arg1)
63+
}
64+
}
65+
66+
/*
67+
go test -benchmem -run=none LeetcodeGolang/Leetcode/0412.Fizz-Buzz -bench=.
68+
69+
*/

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ https://kimi0230.github.io/LeetcodeGolang/
1111
- [leetcode Content](#leetcode-content)
1212
- [Data Structure](#data-structure)
1313
- [Array \& String](#array--string)
14-
- [| 0381 | 0381.Insert Delete GetRandom O(1) Duplicates allowed | Go | Medium | O(1) | O(n) | Array |](#-0381----0381insert-delete-getrandom-o1-duplicates-allowed---go---medium------o1-----------on----array-----------------)
14+
- [| 0412 | 0412.Fizz Buzz | Go | Easy | O(n) | | Array, string |](#-0412----------------------------------------------------------0412fizz-buzz---------------------------------------go---------------------easy------on---------------array-string-----------------)
1515
- [Matrix](#matrix)
1616
- [Linked List](#linked-list)
1717
- [HashSet \& HashMap](#hashset--hashmap)
@@ -73,6 +73,7 @@ https://kimi0230.github.io/LeetcodeGolang/
7373
| [0409](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0409.Longest-Palindrome) | [409. Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0409.Longest-Palindrome) | Easy | O(n) | O(1) | Array |
7474
| [0380](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0380.Insert-Delete-GetRandom-O1) | [0380.Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0380.Insert-Delete-GetRandom-O1) | Medium | O(1) | O(n) | Array |
7575
| [0381](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0381.Insert-Delete-GetRandom-O1-Duplicates-allowed) | [0381.Insert Delete GetRandom O(1) Duplicates allowed](https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0381.Insert-Delete-GetRandom-O1-Duplicates-allowed) | Medium | O(1) | O(n) | Array |
76+
| [0412](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0412.Fizz-Buzz) | [0412.Fizz Buzz](https://leetcode.com/problems/fizz-buzz/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0412.Fizz-Buzz) | Easy | O(n) | | Array, string |
7677
---
7778

7879
#### Matrix

0 commit comments

Comments
 (0)