Problem
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.
Einschränkungen
0 ≤ limit ≤ 10,000. A limit of 0 produces an empty list.
Beispiele
Eingabe:
limit = 5
Erwartete Ausgabe:
expected = ["1", "2", "Fizz", "4", "Buzz"]
Eingabe:
long long limit = 5;
Erwartete Ausgabe:
std::vector<std::string> expected = {"1", "2", "Fizz", "4", "Buzz"};
Eingabe:
long limit = 5;
Erwartete Ausgabe:
string[] expected = new string[] {"1", "2", "Fizz", "4", "Buzz"};
Erläuterung
3 is divisible by 3 and 5 is divisible by 5. Other entries keep their number.
Eingabe:
limit = 0
Erwartete Ausgabe:
expected = []
Eingabe:
long long limit = 0;
Erwartete Ausgabe:
std::vector<std::string> expected = {};
Eingabe:
long limit = 0;
Erwartete Ausgabe:
string[] expected = new string[] {};
Erläuterung
There are no integers from 1 through 0, so the list is empty.
So gehst du vor
Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.
Hinweis 1
Each position depends only on the current integer’s divisibility by 3 and 5.
Hinweis 2
Build the result list in order by checking both divisibility conditions for every number.
Hinweis 3
Handle the lower bound carefully: if the limit is 0, the answer is an empty list.
Erwartete Komplexität
O(n) time, O(n) extra space
Referenzen
Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.