Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Fizz Buzz problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Fizz Buzz problem solution we have given an integer n, return a string array answer (1-indexed) where:

  1. answer[i] == “FizzBuzz” if i is divisible by 3 and 5.
  2. answer[i] == “Fizz” if i is divisible by 3.
  3. answer[i] == “Buzz” if i is divisible by 5.
  4. answer[i] == i if non of the above conditions are true.
Leetcode Fizz Buzz problem solution

Leetcode Fizz Buzz problem solution in Python.

class Solution:
    def fizzBuzz(self, n: int) -> List[str]:
        
        final_list = []
        for i in range(1, n+1):
            
            if i%15 == 0:
                final_list.append("FizzBuzz")
            elif i%5 == 0:
                final_list.append("Buzz")   
            elif i%3 == 0:
                final_list.append("Fizz")
            else:
                final_list.append(str(i))
                
        return final_list

Fizz Buzz problem solution in Java.

class Solution {
    public List<String> fizzBuzz(int n) {
        List<String> result = new ArrayList<String>(); 
        for(int i=1;i<=n;i++){
            if(i%3==0 && i%5==0){
                result.add("FizzBuzz");
                continue;
            }
            
            if(i%3==0){
                result.add("Fizz");
                continue;
            }
            
            if(i%5==0){
                result.add("Buzz");
                continue;
            }
            
            result.add(i+"");
        }
        
        return result;
    }
}

Problem solution in C++.

class Solution {
public:
    vector<string> fizzBuzz(int n) {
        vector<string>ans;
        for(int i=1;i<=n;i++)
        {
            string ap;
            if(i%3==0)
                ap+="Fizz";
            if(i%5==0)
                ap+="Buzz";
            if(ap.length()==0)
                ap=to_string(i);
            ans.push_back(ap);
                
        }
        return ans;
    }
};

Problem solution in C.

char ** fizzBuzz(int n, int* returnSize){

char **res=(char**)calloc(n,sizeof(char *));int c=0;
for(int i=1;i<=n;i++)
{
    if(i%3==0&&i%5==0)
    {
        *(res+c)=(char *)calloc(9,sizeof(char));
        res[c][0]='F';res[c][1]='i';res[c][2]='z';
        res[c][3]='z';res[c][4]='B';res[c][5]='u';
        res[c][6]='z';res[c][7]='z';res[c][8]='';
        c++;
    }
    else if(i%3==0)
    {
        *(res+c)=(char *)calloc(5,sizeof(char));
        res[c][0]='F';res[c][1]='i';res[c][2]='z';
        res[c][3]='z';res[c][4]='';
        c++;
    }
    else if(i%5==0)
    {
        *(res+c)=(char *)calloc(5,sizeof(char));
        res[c][0]='B';res[c][1]='u';res[c][2]='z';
        res[c][3]='z';res[c][4]='';
        c++;
    }
    else
    {
        int x=0,y=i;
        while(y)
        {
            x++;
            y=y/10;
        }
        *(res+c)=(char *)calloc(x+1,sizeof(char));
        res[c][x]='';
        x--;
        y=i;
        while(y)
        {
            int m=y%10;
            res[c][x--]=48+m;
            y=y/10;
        }
        c++;
    }
    
}
*returnSize=c;
return res;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes