Skip to content

Latest commit

 

History

History
105 lines (84 loc) · 2.62 KB

_412. Fizz Buzz.md

File metadata and controls

105 lines (84 loc) · 2.62 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 02, 2024

Last updated : July 01, 2024


Related Topics : Math, String, Simulation

Acceptance Rate : 73.95 %


Solutions

Java

// CAUSE WHY NOT
// It's slower though than the alternative unfortunately :l

class Solution {
    public List<String> fizzBuzz(int n) {
        HashMap<Integer, String> vals = new HashMap<>();
        vals.put(0, "FizzBuzz");
        vals.put(10, "Buzz");
        vals.put(5, "Buzz");
        vals.put(3, "Fizz");
        vals.put(6, "Fizz");
        vals.put(9, "Fizz");
        vals.put(12, "Fizz");

        ArrayList<String> output = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            output.add(vals.getOrDefault(i % 15, "" + i));
        }
        
        return output;
    }
}
class Solution {
    public List<String> fizzBuzz(int n) {
        ArrayList<String> output = new ArrayList<>();

        for (int i = 1; i <= n; i++) {
            if (i % 15 == 0) {
                output.add("FizzBuzz");
            } else if (i % 5 == 0) {
                output.add("Buzz");
            } else if (i % 3 == 0) {
                output.add("Fizz");
            } else {
                output.add("" + i);
            }
        }
        
        return output;
    }
}

Python

# Cause funny hehe

class Solution:
    def fizzBuzz(self, n: int) -> List[str]:
        return ['FizzBuzz' if x % 15 == 0 else 'Fizz' if x % 3 == 0 else 'Buzz' if x % 5 == 0 else str(x) for x in range(1, n + 1)]
class Solution:
    def fizzBuzz(self, n: int) -> List[str]:
        output = []

        for i in range(1, n + 1) :
            if i % 15 == 0 :
                output.append('FizzBuzz')
            elif i % 5 == 0 :
                output.append('Buzz')
            elif i % 3 == 0 :
                output.append('Fizz')
            else :
                output.append(f'{i}')
        
        return output