-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFizz Buzz.py
50 lines (36 loc) · 1.03 KB
/
Fizz Buzz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from typing import List
class Solution:
"""
Time: O(n)
Memory: O(1)
"""
FIZZ = 3
BUZZ = 5
def fizzBuzz(self, n: int) -> List[str]:
nums = [str(num) for num in range(1, n + 1)]
for fizz in range(self.FIZZ - 1, n, self.FIZZ):
nums[fizz] = 'Fizz'
for buzz in range(self.BUZZ - 1, n, self.BUZZ):
nums[buzz] = 'Buzz'
for fizzbuzz in range(self.FIZZ * self.BUZZ - 1, n, self.FIZZ * self.BUZZ):
nums[fizzbuzz] = 'FizzBuzz'
return nums
class Solution:
"""
Time: O(n)
Memory: O(1)
"""
FIZZ = 3
BUZZ = 5
def fizzBuzz(self, n: int) -> List[str]:
nums = []
for num in range(1, n + 1):
if not num % (self.FIZZ * self.BUZZ):
nums.append('FizzBuzz')
elif not num % self.FIZZ:
nums.append('Fizz')
elif not num % self.BUZZ:
nums.append('Buzz')
else:
nums.append(str(num))
return nums