MUYANG GUO / INDEX

LeetCode

LeetCode 412 Fizz Buzz - Easy

412. Fizz Buzz -- Easy

·1 min read·#LeetCode#Easy#Python

412. Fizz Buzz — Easy

Open on LeetCode

Problem

  1. Fizz Buzz -- Easy

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

class Solution:
    def fizzBuzz(self, n: int) -> List[str]:
        """
        :type n: int
        :rtype: List[str]
        """
        # ans list
        ans = []
 
        for num in range(1,n+1):
 
            divisible_by_3 = (num % 3 == 0)
            divisible_by_5 = (num % 5 == 0)
 
            if divisible_by_3 and divisible_by_5:
                # Divides by both 3 and 5, add FizzBuzz
                ans.append("FizzBuzz")
            elif divisible_by_3:
                # Divides by 3, add Fizz
                ans.append("Fizz")
            elif divisible_by_5:
                # Divides by 5, add Buzz
                ans.append("Buzz")
            else:
                # Not divisible by 3 or 5, add the number
                ans.append(str(num))
 
        return ans

Comments