Description
Given an integer n
, return a string array answer
(1-indexed) where:
answer[i] == "FizzBuzz"
ifi
is divisible by3
and5
.answer[i] == "Fizz"
ifi
is divisible by3
.answer[i] == "Buzz"
ifi
is divisible by5
.answer[i] == i
(as a string) if none of the above conditions are true.
Examples:
Example 1:
Input: n = 3
Output: ["1","2","Fizz"]
Example 2:
Input: n = 5
Output: ["1","2","Fizz","4","Buzz"]
Example 3:
Input: n = 15
Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
Solution in Python
Approach:
- Iterate Over Numbers:
- Iterate through numbers from 1 to n.
- Check Conditions:
- For each number:
- If divisible by both 3 and 5, append
"FizzBuzz"
to the result. - If divisible by 3, append
"Fizz"
. - If divisible by 5, append
"Buzz"
. - Otherwise, append the number as a string.
- If divisible by both 3 and 5, append
- For each number:
- Output Result:
- Return the resulting list.
Python
class Solution:
def fizzBuzz(self, n: int) -> List[str]:
# Initialize the result array
result = []
# Iterate over each number from 1 to n
for i in range(1, n + 1):
# Check divisibility conditions
if i % 3 == 0 and i % 5 == 0:
result.append("FizzBuzz")
elif i % 3 == 0:
result.append("Fizz")
elif i % 5 == 0:
result.append("Buzz")
else:
result.append(str(i)) # Convert number to string and append
# Return the final result
return result
Detailed Explanation:
- Initialization:
- Create an empty list
result
to store the final output.
- Create an empty list
- Loop Through Numbers:
- Use a
for
loop from 1 to n (inclusive).
- Use a
- Check Divisibility:
- Divisible by 3 and 5: Append
"FizzBuzz"
. - Divisible by 3 only: Append
"Fizz"
. - Divisible by 5 only: Append
"Buzz"
. - None of the Above: Convert the number to a string using
str(i)
and append it.
- Divisible by 3 and 5: Append
- Output:
- The result list contains the FizzBuzz representation of numbers from 1 to n.
Solution in C++
C++
class Solution {
public:
vector<string> fizzBuzz(int n) {
// Create a vector to store the resulting strings
vector<string> answer;
// Iterate through numbers from 1 to n
for (int i = 1; i <= n; ++i) {
// Check divisibility conditions
if (i % 3 == 0 && i % 5 == 0) {
// If divisible by both 3 and 5, append "FizzBuzz"
answer.push_back("FizzBuzz");
} else if (i % 3 == 0) {
// If divisible only by 3, append "Fizz"
answer.push_back("Fizz");
} else if (i % 5 == 0) {
// If divisible only by 5, append "Buzz"
answer.push_back("Buzz");
} else {
// If none of the above conditions are met, append the number as a string
answer.push_back(to_string(i));
}
}
// Return the filled vector
return answer;
}
};
Solution in Javascript
JavaScript
var fizzBuzz = function(n) {
// Initialize an empty array to store the result
let answer = [];
// Iterate from 1 to n (inclusive)
for (let i = 1; i <= n; i++) {
// Check if the current number i is divisible by both 3 and 5
if (i % 3 === 0 && i % 5 === 0) {
answer.push("FizzBuzz"); // Add "FizzBuzz" to the result array
}
// Check if the current number i is divisible by 3
else if (i % 3 === 0) {
answer.push("Fizz"); // Add "Fizz" to the result array
}
// Check if the current number i is divisible by 5
else if (i % 5 === 0) {
answer.push("Buzz"); // Add "Buzz" to the result array
}
// If none of the above conditions are true
else {
answer.push(i.toString()); // Convert the number to a string and add it to the result array
}
}
// Return the final result array
return answer;
};
Solution in Java
Java
class Solution {
public List<String> fizzBuzz(int n) {
// Create a list to store the results
List<String> result = new ArrayList<>();
// Loop through numbers from 1 to n
for (int i = 1; i <= n; i++) {
// Check if the current number is divisible by both 3 and 5
if (i % 3 == 0 && i % 5 == 0) {
result.add("FizzBuzz"); // Add "FizzBuzz" to the list
}
// Check if the current number is divisible by 3
else if (i % 3 == 0) {
result.add("Fizz"); // Add "Fizz" to the list
}
// Check if the current number is divisible by 5
else if (i % 5 == 0) {
result.add("Buzz"); // Add "Buzz" to the list
}
// If none of the above conditions are met
else {
result.add(String.valueOf(i)); // Add the number as a string
}
}
// Return the resulting list
return result;
}
}