Fizz Buzz
Easy · simulation
Given a whole number n, return a list of n strings. The string at position i (counting from 1, so the first string is for 1 and the last is for n) follows these rules:
- If
iis a multiple of both 3 and 5, the string is"FizzBuzz". - Otherwise, if
iis a multiple of 3, the string is"Fizz". - Otherwise, if
iis a multiple of 5, the string is"Buzz". - Otherwise, the string is the number
iwritten as text, for example"7".
The list is in order, starting from 1.
Examples
Example 1
Input: n = 5
Output: ["1", "2", "Fizz", "4", "Buzz"]
3 is a multiple of 3 and 5 is a multiple of 5. The others are just their own numbers.
Example 2
Input: n = 15
Output: ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]
15 is a multiple of both 3 and 5, so it becomes FizzBuzz.
Example 3
Input: n = 1
Output: ["1"]
Constraints
- 1 <= n <= 2000