समस्या
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.
उदाहरण
इनपुट:
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.
इनपुट:
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
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 के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।