#412

Fizz Buzz

Easy Strings & List

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

For numbers 1 to n, produce a list of strings: "FizzBuzz" for multiples of both 3 and 5, "Fizz" for multiples of 3, "Buzz" for multiples of 5, and the number itself otherwise.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: label each number 1..n by whether 3 and/or 5 divide it.

Why this works: the % operator tests divisibility. Checking 15 first makes sure "FizzBuzz" wins over plain "Fizz".

Example: n = 6

1 → "1", 2 → "2", 3 → "Fizz", 4 → "4", 5 → "Buzz", 6 → "Fizz"

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Checking i % 3 before i % 15, so 15 prints "Fizz" instead of "FizzBuzz".