# Description

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

import java.util.ArrayList;
import java.util.List;

/**
* 412. FizzBuzz
*
* @author kimtaeng
* created on 2018. 1. 10.
*/

public class Solution {
public List<String> fizzBuzz(int n) {
List<String> list = new ArrayList<String>();
for (int i = 1; i <= n; i++) {
String s = "";
if (i % 3 == 0) s = "Fizz";
if (i % 5 == 0) s += "Buzz";
else if (i % 3 != 0) s = i + "";
}
return list;
}
}


Not using “%” Operation.

import java.util.ArrayList;
import java.util.List;

/*
* 412. FizzBuzz
*
* @author kimtaeng
* created on 2018. 1. 10.
*/
public class Solution {
public List<String> fizzBuzz(int n) {
List<String> resultList = new ArrayList<String>();
for (int i = 1, fizz = 0, buzz = 0; i <= n; i++) {
fizz++;
buzz++;
if(fizz == 3 && buzz == 5) {
fizz = 0;
buzz = 0;
} else if (fizz == 3) {
fizz = 0;
} else if (buzz == 5) {
buzz = 0;
} else {
}
}
return resultList;
}
}


댓글을 남기시려면 Github 로그인을 해주세요 :D

Hi, there!

Thanks for visiting my blog.
Please let me know if there are any mistakes in my post.