-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path412. Fizz Buzz.java
72 lines (62 loc) · 1.7 KB
/
412. 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
Write a program that outputs the string representation of numbers from 1 to n.
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
Example:
n = 15,
Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]
*/
class Solution {
// // 法一: ✅ 用了求余数的操作,CPU效率极低 99.87%
// public List<String> fizzBuzz(int n) {
// List<String> res = new ArrayList<>();
// for(int i=0; i<n; i++){
// if((i+1)%3==0 && (i+1)%5!=0) res.add("Fizz");
// else if((i+1)%3!=0 && (i+1)%5==0) res.add("Buzz");
// else if((i+1)%3==0 && (i+1)%5==0) res.add("FizzBuzz");
// else res.add(Integer.toString(i+1));
// }
// return res;
// }
// 法二:✅ 不用求余的操作 99.87%
public List<String> fizzBuzz(int n) {
List<String> res = new ArrayList<>();
int buzz = 0; int fizz = 0;
for(int i=0; i<n; i++){
buzz ++;
fizz ++;
// fb ++;
if(fizz == 3 && buzz == 5){
res.add("FizzBuzz");
fizz = 0;
buzz = 0;
}
else if(fizz==3) {
res.add("Fizz");
fizz = 0;
}
else if(buzz==5) {
res.add("Buzz");
buzz = 0;
}
else res.add(Integer.toString(i+1));
}
return res;
}
}