簡単 0件解決済み 正答率0%

Fizz Buzz

Build a sequence using divisibility by 3 and 5.

このチャレンジで学べること

  • Test divisibility by 3 and 5
  • Order if/else branches correctly
  • Build a list of results in a loop

問題

翻訳を表示

For each integer from 1 through limit, use Fizz for multiples of 3, Buzz for multiples of 5, and FizzBuzz for multiples of both. Otherwise use the number written as text. Return the entries in order instead of printing them.

Implement fizz_buzz(limit).

Input

limit: The last integer to include.

Output

Return a list of strings, one for each integer from 1 through limit.

制約

0 ≤ limit ≤ 10,000. A limit of 0 produces an empty list.

例

例 1
入力:
limit = 5

期待される出力:
expected = ["1", "2", "Fizz", "4", "Buzz"]
入力:
long long limit = 5;

期待される出力:
std::vector<std::string> expected = {"1", "2", "Fizz", "4", "Buzz"};
入力:
long limit = 5;

期待される出力:
string[] expected = new string[] {"1", "2", "Fizz", "4", "Buzz"};

解説

3 is divisible by 3 and 5 is divisible by 5. Other entries keep their number.

例 2
入力:
limit = 0

期待される出力:
expected = []
入力:
long long limit = 0;

期待される出力:
std::vector<std::string> expected = {};
入力:
long limit = 0;

期待される出力:
string[] expected = new string[] {};

解説

There are no integers from 1 through 0, so the list is empty.

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

Each position depends only on the current integer’s divisibility by 3 and 5.

ヒント 2

Build the result list in order by checking both divisibility conditions for every number.

ヒント 3

Handle the lower bound carefully: if the limit is 0, the answer is an empty list.

目安の計算量

O(n) time, O(n) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。