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", "fizz", "4", "buzz", "fizz", "7", "8", "fizz", "buzz", "11", "fizz", "13", "14", "fizz buzz"]

StringBuffer的用法,append方法的参数可以使boolean, char, char[], CharSequence, double, float, int, long, String, StringBuffer

 1 class Solution {
 2     /**
 3      * param n: As description.
 4      * return: A list of strings.
 5      */
 6     public ArrayList<String> fizzBuzz(int n) {
 7         ArrayList<String> res = new ArrayList<String>();
 8         for (int i=1; i<=n; i++) {
 9             StringBuffer buf = new StringBuffer();
10             if (i%3==0 && i%5!=0) {
11                 buf.append("fizz");
12             }
13             if (i%3!=0 && i%5==0) {
14                 buf.append("buzz");
15             }
16             if (i%3==0 && i%5==0) {
17                 buf.append("fizz buzz");
18             }
19             if (i%3!=0 && i%5!=0) {
20                 buf.append(i);
21             }
22             res.add(buf.toString());
23         }
24         return res;
25     }
26 }
时间: 2024-10-08 15:34:04

Lintcode: Fizz Buzz的相关文章

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

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

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 问题

题目来源:http://www.lintcode.com/zh-cn/problem/fizz-buzz/ 可以accept的程序如下: 1 class Solution { 2 public: 3 /** 4 * param n: As description. 5 * return: A list of strings. 6 */ 7 vector<string> fizzBuzz(int n) { 8 vector<string> results; 9 for (int i

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

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

Fizz Buzz 问题

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

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 bot