Fizz Buzz - Leetcode 412 - Java

preview_player
Показать описание
Learn how to solve the Leetcode problem of id 412, whose title is Fizz Buzz, using the Java programming language.

The Data Structures and Algorithms (DSA) lesson uses an iterative approach to solve the problem.

You can check whether a number is divisible by 3 if you use the modulo or remainder operator, usually represented by a percent sign.

A number will be divisible if there is no remainder. That is, the result is 0.

Similarly, you can use the number followed by percent sign followed by five. The result will be zero if divisible by 5.

Alternatively, you can also directly check a number is divisible by the product of 3 and 5, that is 15. Since mathematically a number divisible by 3 and by 5 must be divisible by 15.

The problem uses an if-else-if-else statement to take care of the cases.

First the case for divisible by both 3 and 5. Add FizzBuzz string to the list.

Then the case where only divisible by 3. Add Fizz string to the list.

Then the case where only divisible by 5. Add Buzz string to the list.

Then the case where none of the conditions above were satisfied. In that case, simply add the iterating index value as a string to the list.

The time complexity for the solution is O(n) and its space complexity is O(n).

DSA problems are sometimes asked during tech job interviews for positions such as Software Engineer, so you can use the challenge to practice that skill.
Рекомендации по теме