DAY 7
1
Software Development

## 412. FizzBuzz

412. FizzBuzz 是一個相當經典的題目，號稱是 Google 面題題 之一，這個題目雖然看似簡單的但有許多細節值得深究。

### 先看一下題目描述

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 if non of the above conditions are true.
``````

### 再搭配範例理解題目

• Example 1:
``````Input: n = 3
Output: ["1","2","Fizz"]
``````
• Example 2:
``````Input: n = 15
Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
``````

## 開始實作

### 方法 ①：暴力法

#### 那我們先用 Python 實作看看

``````class Solution:
def fizzBuzz(self, n):
ans = []

for num in range(1,n+1):
if num % 3 == 0 and num % 5 == 0:
ans.append("FizzBuzz")
elif num % 3 == 0:
ans.append("Fizz")
elif num % 5 == 0:
ans.append("Buzz")
else:
ans.append(str(num))

return ans
``````

#### 也可以用 JavaScript 復刻一次

``````var fizzBuzz = function(n) {
let ans = [];

for (let i = 1; i <= n; i++) {
if (i % 15 === 0) {
ans.push('FizzBuzz');
} else if (i % 5 === 0) {
ans.push('Buzz');
} else if (i % 3 === 0) {
ans.push('Fizz');
} else {
ans.push(i + '');
}
}
return ans;
};
``````

### 方法 ②：字串組裝

1. Divisible by 3
2. Divisible by 5
3. Divisible by 7
4. Divisible by 3 and 5
5. Divisible by 3 and 7
6. Divisible by 7 and 3
7. Divisible by 3 and 5 and 7
8. Not divisible by 3 or 5 or 7.

1. Divisible by 3 => Fizz
2. Divisible by 3 and 5 => FizzBuzz
3. Divisible by 3 and 7 => FizzJazz
4. Divisible by 3 and 5 and 7 => FizzBuzzJazz

``````s = ''
if num % 3 == 0:
s += "Fizz"
if num % 5 == 0:
s += "Buzz"
``````

#### 那我們先用 Python 實作看看

``````
class Solution:
def fizzBuzz(self, n):
ans = []

for num in range(1,n+1):

s = ""

if num % 3 == 0:
s += "Fizz"
if num % 5 == 0:
s += "Buzz"
if not num_ans_str:
s = str(num)

ans.append(s)

return ans
``````

#### 也可以用 JavaScript 復刻一次

``````var fizzBuzz = function(n) {

let ans = [];

for (let i = 1; i <= n; i++) {

let s = '';

if (i % 3 === 0)
s += 'Fizz';
if (i % 5 === 0)
s += 'Buzz';
if (!str)
s = i + '';

ans.push(s);
}

return ans
};
``````

### 方法 ③：Hashing

``````d = {3 : "Fizz", 5 : "Buzz"}
for key in d.keys():
if num % key == 0:
s += d[key]
``````

#### 那我們先用 Python 實作看看

``````class Solution:
def fizzBuzz(self, n):

ans = []
d = {3 : "Fizz", 5 : "Buzz"}

for num in range(1,n+1):
s = ""

for key in d.keys():
if num % key == 0:
s += d[key]
if not num_ans_str:
s = str(num)

ans.append(s)

return ans
``````

#### 也可以用 JavaScript 復刻一次

``````let fizzBuzz = function(n) {

let ans = [];
let d = { 3: 'Fizz', 5: 'Buzz', ...};

for (let i = 1; i <= n; i++) {
let s = '';
for (let key in d) {
if (i % parseInt(key, 10) === 0) {
s += d[key];
}
}

if (!s) {
s += i;
}

ans.push(s);
}

return result;
};
``````

### 方法 ④：炫技法

#### 那我們先用 Python 實作看看

``````class Solution:
def fizzBuzz(self, n):
ans = []
for n in range(1, N+1):
ans.append(int(not(n % 3)) * 'Fizz' + int(not(n % 5)) * 'Buzz' or n)
return ans
``````

#### 也可以用 JavaScript 復刻一次

``````var fizzBuzz = function(n) {
let ans = [];
for (let i = 1; i <= n; i++) {
s = ((i % 3 == 0 ? 'Fizz' : '') + (i % 5 == 0 ? 'Buzz' : '')) || (i + '');
ans.push(s);
}

return ans
};
``````

## 反思沉澱

### 2 則留言

0
Ellen Lee
iT邦新手 5 級 ‧ 2021-09-22 21:19:16

0
TD
iT邦新手 4 級 ‧ 2021-09-22 23:00:28
``````var fizzBuzz = function(n) {

let ans = [];

for (let i = 1; i <= n; i++) {

let s = '';

if (i % 3 === 0)
s += 'Fizz,';
if (i % 5 === 0)
s += 'Buzz,';
if (!str)
str = i + '';

ans.push(str);
}

return ans
};
``````

``````var fizzBuzz = function(n) {

let ans = [];

for (let i = 1; i <= n; i++) {

let str = '';

if (i % 3 === 0)
str += 'Fizz';
if (i % 5 === 0)
str += 'Buzz';
if (!str)
str = i + '';

ans.push(str);
}

return ans
};
``````