Skip to content

Commit 1229f12

Browse files
committed
O(n) time and O(1) space
1 parent ff58fa9 commit 1229f12

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

412. Fizz Buzz/412. Fizz Buzz.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Write a program that outputs the string representation of numbers from 1 to n.
3+
4+
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”.
5+
6+
Example:
7+
8+
n = 15,
9+
10+
Return:
11+
[
12+
"1",
13+
"2",
14+
"Fizz",
15+
"4",
16+
"Buzz",
17+
"Fizz",
18+
"7",
19+
"8",
20+
"Fizz",
21+
"Buzz",
22+
"11",
23+
"Fizz",
24+
"13",
25+
"14",
26+
"FizzBuzz"
27+
]
28+
"""
29+
class Solution:
30+
def fizzBuzz(self, n: int) -> List[str]:
31+
res = []
32+
for i in range(1,n+1):
33+
temp_str = ''
34+
if i % 3 == 0:
35+
temp_str += 'Fizz'
36+
if i % 5 == 0:
37+
temp_str += 'Buzz'
38+
if i % 3 != 0 and i %5 != 0:
39+
temp_str += str(i)
40+
res.append(temp_str)
41+
return res
42+

0 commit comments

Comments
 (0)