[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 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”

code

 1 class Solution {
 2     public List<String> fizzBuzz(int n) {
 3         List<String>  result = new ArrayList<>();
 4         for(int i = 1; i <= n; i++){
 5             if(i % 3 == 0 && i % 5 == 0){
 6                 result.add("FizzBuzz");
 7             }else if(i % 3 == 0 ){
 8                 result.add("Fizz");
 9             }else if(i % 5 == 0 ){
10                 result.add("Buzz");
11             }else{   
12                 result.add(i + "");
13             }
14         }
15         return result;    
16     }
17 }
原文地址:https://www.cnblogs.com/liuliu5151/p/10873413.html