-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fizz Buzz.java
45 lines (41 loc) · 956 Bytes
/
Fizz Buzz.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
Given number n. Print number from 1 to n. But:
when number is divided by 3, print "fizz".
when number is divided by 5, print "buzz".
when number is divided by both 3 and 5, print "fizz buzz".
Example
If n = 15, you should return:
[
"1", "2", "fizz",
"4", "buzz", "fizz",
"7", "8", "fizz",
"buzz", "11", "fizz",
"13", "14", "fizz buzz"
]
*/
class Solution {
/**
* param n: As description.
* return: A list of strings.
*/
public ArrayList<String> fizzBuzz(int n) {
ArrayList<String> result= new ArrayList<String>();
for(int i=1;i<=n;i++)
{
if(i%15==0)
{
result.add("fizz buzz");
}else if(i%3==0)
{
result.add("fizz");
}else if(i%5==0)
{
result.add("buzz");
}else
{
result.add(String.valueOf(i));
}
}
return result;
}
}