339. Fizz Buzz
Given a positive integer n, build the classic counting game as a list of strings: for every i from 1 to n (in order), the entry is
"FizzBuzz"wheniis divisible by both 3 and 5,"Fizz"wheniis divisible by 3 only,"Buzz"wheniis divisible by 5 only,- otherwise the number
iitself, written as a string.
Return the list of n strings. Capitalization matters: exactly Fizz, Buzz, and FizzBuzz.
Example 1:
Input: n = 3
Output: ["1", "2", "Fizz"]
Explanation: 1 and 2 divide by neither 3 nor 5, so they stay as numbers; 3 divides by 3, so it becomes Fizz.
Example 2:
Input: n = 5
Output: ["1", "2", "Fizz", "4", "Buzz"]
Explanation: 3 becomes Fizz and 5 becomes Buzz; everything else keeps its numeric form.
Example 3:
Input: n = 15
Output: ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]
Explanation: 15 is the first number divisible by both 3 and 5, so it becomes FizzBuzz rather than Fizz or Buzz alone.
Constraints:
- 1 ≤ n ≤ 10⁴
Hints:
The percent operator answers divisibility: `i % 3 == 0` means i is a multiple of 3. Loop i from 1 to n and pick one of the four cases.
Branch order matters. If you test `i % 3` before the combined case, 15 prints Fizz and never reaches FizzBuzz — check divisibility by both first, or build the word by concatenation so no combined branch is needed.
▶ Run checks these sample cases. Submit also runs hidden edge cases.
Input: n = 3
Expected output: ["1", "2", "Fizz"]