-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFizzBuzz.java
45 lines (39 loc) · 860 Bytes
/
FizzBuzz.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
package integer;
import java.util.*;
/**
* @author Shogo Akiyama
* Solved on 06/19/2019 and 06/19/2020
*
* 412. Fizz Buzz
* https://leetcode.com/problems/fizz-buzz/
* Difficulty: Easy
*
* Approach: Iteration
* Runtime: 2 ms, faster than 22.32% of Java online submissions for Fizz Buzz.
* Memory Usage: 37.3 MB, less than 99.95% of Java online submissions for Fizz Buzz.
*
* Time Complexity: O(n)
* Space Complexity: O(1)
* Where n is the given number
*
* @see IntegerTest#testFizzBuzz()
*/
public class FizzBuzz {
public List<String> fizzBuzz(int n) {
List<String> list = new ArrayList<String>();
for (int i = 1; i <= n; i++) {
String s = "";
if (i % 3 == 0) {
s += "Fizz";
}
if (i % 5 == 0) {
s += "Buzz";
}
if (s.equals("")) {
s = String.valueOf(i);
}
list.add(s);
}
return list;
}
}