-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path412.fizz-buzz.java
More file actions
74 lines (71 loc) · 1.41 KB
/
Copy path412.fizz-buzz.java
File metadata and controls
74 lines (71 loc) · 1.41 KB
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
73
/*
* @lc app=leetcode.cn id=412 lang=java
*
* [412] Fizz Buzz
*
* https://leetcode-cn.com/problems/fizz-buzz/description/
*
* algorithms
* Easy (67.40%)
* Likes: 115
* Dislikes: 0
* Total Accepted: 79.2K
* Total Submissions: 116.1K
* Testcase Example: '3'
*
* 写一个程序,输出从 1 到 n 数字的字符串表示。
*
* 1. 如果 n 是3的倍数,输出“Fizz”;
*
* 2. 如果 n 是5的倍数,输出“Buzz”;
*
* 3.如果 n 同时是3和5的倍数,输出 “FizzBuzz”。
*
* 示例:
*
* n = 15,
*
* 返回:
* [
* "1",
* "2",
* "Fizz",
* "4",
* "Buzz",
* "Fizz",
* "7",
* "8",
* "Fizz",
* "Buzz",
* "11",
* "Fizz",
* "13",
* "14",
* "FizzBuzz"
* ]
*
*
*/
import java.util.List;
import java.util.stream.Collectors;
// @lc code=start
class Solution {
public List<String> fizzBuzz(int n) {
List<String> answer = new ArrayList<String>();
for (int i = 1; i <= n; i++) {
StringBuffer sb = new StringBuffer();
if (i % 3 == 0) {
sb.append("Fizz");
}
if (i % 5 == 0) {
sb.append("Buzz");
}
if (sb.length() == 0) {
sb.append(i);
}
answer.add(sb.toString());
}
return answer;
}
}
// @lc code=end