본문 바로가기
LeetCode/Top Interview Q. - Easy

LeetCode. Fizz Buzz [Math]

by 벤진[Benzene] 2020. 6. 13.

Problem:

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" ]

 

-Summary-

1. if number i is divisible by both, we append 'FizzBuzz' to the list.

2. if number i is divisible by only 3, we append 'Fizz' to the list.

3. if number i is divisible by only 5, we append 'Buzz' to the list.

4. if number i is not divisible by 3 and 5, then we append string of the number to the list.

모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]

댓글