<LeetCode> 412. Fizz Buzz
2017-10-16 19:03
351 查看
412. Fizz Buzz
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:
题意:
输出1~n之间的数,其中遇3的倍数输出Fizz,遇5倍数输出Buzz,遇15的倍数输出FizzBuzz。
思路就很简单了
class Solution {
public List<String> fizzBuzz(int n) {
//int n = 50;
List<String> list = new ArrayList<String>();
for (int i = 1; i <= n; i++) {
if(i%3==0&&i%5!=0){
list.add("Fizz");
}else if(i%3!=0&&i%5==0){
list.add("Buzz");
}else if(i%15==0){
list.add("FizzBuzz");
}else {
list.add(Integer.toString(i));
}
}
return list;
}
}
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" ]
题意:
输出1~n之间的数,其中遇3的倍数输出Fizz,遇5倍数输出Buzz,遇15的倍数输出FizzBuzz。
思路就很简单了
class Solution {
public List<String> fizzBuzz(int n) {
//int n = 50;
List<String> list = new ArrayList<String>();
for (int i = 1; i <= n; i++) {
if(i%3==0&&i%5!=0){
list.add("Fizz");
}else if(i%3!=0&&i%5==0){
list.add("Buzz");
}else if(i%15==0){
list.add("FizzBuzz");
}else {
list.add(Integer.toString(i));
}
}
return list;
}
}
相关文章推荐
- <LeetCode> 题3:统计数字
- <LeetCode> 题35:翻转链表1(翻转整个链表)
- <LeetCode><Easy>223 Rectange Area
- <LeetCode><Easy> 168 Excel Sheet Column Title
- <LeetCode><Hard> 4 Median of Two Sorted Arrays
- <LeetCode><Easy> 20 Valid Parenthese
- <LeetCode> 题41:最大子数组
- <LeetCode> 题57:三数之和
- <LeetCode><Easy>258Add Digits
- <LeetCode> Single Number
- <LeetCode><Easy> 21 MergeTwoSortedLists
- <LeetCode><Easy> 100 Same Tree
- <LeetCode><Medium> 3 Longest Substring Without Repeating Characters
- <LeetCode><Easy> 203 Remove Linked List Elements
- <leetcode>617. Merge Two Binary Trees
- <LeetCode> 题40:两个栈实现一个队列
- <leetcode>String to Integer (atoi)
- <LeetCode><Medium> 120 Triangle #维特比译码变形
- <LeetCode><Easy> 228 Summary Ranges
- <LeetCode><Easy> 160 Intersection Of Two Listed Lists