Leetcode: 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:

n = 15,

Return:
[
    "1",
    "2",
    "Fizz",
    "4",
    "Buzz",
    "Fizz",
    "7",
    "8",
    "Fizz",
    "Buzz",
    "11",
    "Fizz",
    "13",
    "14",
    "FizzBuzz"
]

Solution 1:

 1 public class Solution {
 2     public List<String> fizzBuzz(int n) {
 3         List<String> res = new ArrayList<String>();
 4         for (int i=1; i<=n; i++) {
 5             if (i%3==0 && i%5==0) res.add("FizzBuzz");
 6             else if (i % 3 == 0) res.add("Fizz");
 7             else if (i % 5 == 0) res.add("Buzz");
 8             else res.add(Integer.toString(i));
 9         }
10         return res;
11     }
12 }

Solution with out using %

 1 public class Solution {
 2     public List<String> fizzBuzz(int n) {
 3         List<String> ret = new ArrayList<String>(n);
 4         for(int i=1,fizz=0,buzz=0;i<=n ;i++){
 5             fizz++;
 6             buzz++;
 7             if(fizz==3 && buzz==5){
 8                 ret.add("FizzBuzz");
 9                 fizz=0;
10                 buzz=0;
11             }else if(fizz==3){
12                 ret.add("Fizz");
13                 fizz=0;
14             }else if(buzz==5){
15                 ret.add("Buzz");
16                 buzz=0;
17             }else{
18                 ret.add(String.valueOf(i));
19             }
20         }
21         return ret;
22     }
23 }
时间: 2024-10-10 22:10:33

Leetcode: Fizz Buzz的相关文章

[LeetCode] 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 fiv

LeetCode - Fizz Buzz Multithreaded

Write a program that outputs the string representation of numbers from 1 to n, however: If the number is divisible by 3, output "fizz". If the number is divisible by 5, output "buzz". If the number is divisible by both 3 and 5, output

LeetCode 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 bot

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 fiv

Lintcode 9.Fizz Buzz 问题

------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of strings. */ public ArrayList<String> fizzBuzz(int n) { ArrayList<String> results = new ArrayList<String>(); for (int i = 1; i <= n; i++

LintCode (9)Fizz Buzz

下面是AC代码,C++风格: 1 class Solution { 2 public: 3 vector<string> fizzBuzz(int N) { 4 vector<string> Answer; 5 for(int i = 1;i <= N;i++) { 6 if(i % 15 == 0) { 7 Answer.push_back("fizz buzz"); 8 } else if(i % 3 == 0) { 9 Answer.push_bac

Lintcode: Fizz Buzz

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", &qu

Lintcode9 Fizz Buzz solution 题解

[题目描述] 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". 给你一个整数n. 从 1 到 n 按照下面的规则打印

Fizz Buzz 问题

Fizz Buzz 问题 给你一个整数n. 从 1 到 n 按照下面的规则打印每个数: 如果这个数被3整除,打印fizz. 如果这个数被5整除,打印buzz. 如果这个数能同时被3和5整除,打印fizz buzz. 样例 比如 n = 15, 返回一个字符串数组: [ "1", "2", "fizz", "4", "buzz", "fizz", "7", "