-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFizz_Buzz.java
More file actions
27 lines (22 loc) · 763 Bytes
/
Fizz_Buzz.java
File metadata and controls
27 lines (22 loc) · 763 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Given an integer n, return a string array answer (1-indexed) where:
// answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
// answer[i] == "Fizz" if i is divisible by 3.
// answer[i] == "Buzz" if i is divisible by 5.
// answer[i] == i (as a string) if none of the above conditions are true.
class Solution {
public List<String> fizzBuzz(int n) {
ArrayList<String> s = new ArrayList<>();
for(int i=1;i<=n;i++){
if(i % 3 == 0 && i % 5 == 0){
s.add("FizzBuzz");
} else if(i % 3 == 0){
s.add("Fizz");
} else if(i % 5 == 0){
s.add("Buzz");
} else{
s.add(String.valueOf(i));
}
}
return s;
}
}