Header Ad

Leetcode Fizz Buzz problem solution

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


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



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]='\0';
        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]='\0';
        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]='\0';
        c++;
    }
    else
    {
        int x=0,y=i;
        while(y)
        {
            x++;
            y=y/10;
        }
        *(res+c)=(char *)calloc(x+1,sizeof(char));
        res[c][x]='\0';
        x--;
        y=i;
        while(y)
        {
            int m=y%10;
            res[c][x--]=48+m;
            y=y/10;
        }
        c++;
    }
    
}
*returnSize=c;
return res;
}


Post a Comment

0 Comments