Fizz Buzz - List - Easy - LeetCode
💻 coding

Fizz Buzz - List - Easy - LeetCode

1 min read 112 words
1 min read
ShareWhatsAppPost on X
  • 1The Fizz Buzz program outputs numbers from 1 to n, replacing multiples of three with 'Fizz' and multiples of five with 'Buzz'.
  • 2For numbers that are multiples of both three and five, the program outputs 'FizzBuzz'.
  • 3The algorithm has a time complexity of O(n) and a space complexity of O(n).

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The Fizz Buzz program outputs numbers from 1 to n, replacing multiples of three with 'Fizz' and multiples of five with 'Buzz'."

Fizz Buzz - List - Easy - LeetCode

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

public class Solution {
 public IList<string> FizzBuzz(int n) {
 var res = new List<string>();
 for(int i=1;i<=n;i++){
 if(i%3==0 && i%5==0){
 res.Add("FizzBuzz");
 }
 else if(i%3==0){
 res.Add("Fizz");
 }
 else if(i%5==0){
 res.Add("Buzz");
 }
 else{
 res.Add(i.ToString());
 } 
 }
 
 return res;
 }
}

Time Complexity: O(n)

Space Complexity: O(n)

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

sumitc91

Published on 16 October 2020 · 1 min read · 112 words

Part of AskGif Blog · coding

You might also like

Fizz Buzz - List - Easy - LeetCode | AskGif Blog