Skip to content

Commit a9905f5

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 18.4 MB (11.78%)
1 parent 9e14bc2 commit a9905f5

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

0412-fizz-buzz/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<p>Given an integer <code>n</code>, return <em>a string array </em><code>answer</code><em> (<strong>1-indexed</strong>) where</em>:</p>
2+
3+
<ul>
4+
<li><code>answer[i] == &quot;FizzBuzz&quot;</code> if <code>i</code> is divisible by <code>3</code> and <code>5</code>.</li>
5+
<li><code>answer[i] == &quot;Fizz&quot;</code> if <code>i</code> is divisible by <code>3</code>.</li>
6+
<li><code>answer[i] == &quot;Buzz&quot;</code> if <code>i</code> is divisible by <code>5</code>.</li>
7+
<li><code>answer[i] == i</code> (as a string) if none of the above conditions are true.</li>
8+
</ul>
9+
10+
<p>&nbsp;</p>
11+
<p><strong class="example">Example 1:</strong></p>
12+
<pre><strong>Input:</strong> n = 3
13+
<strong>Output:</strong> ["1","2","Fizz"]
14+
</pre><p><strong class="example">Example 2:</strong></p>
15+
<pre><strong>Input:</strong> n = 5
16+
<strong>Output:</strong> ["1","2","Fizz","4","Buzz"]
17+
</pre><p><strong class="example">Example 3:</strong></p>
18+
<pre><strong>Input:</strong> n = 15
19+
<strong>Output:</strong> ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
20+
</pre>
21+
<p>&nbsp;</p>
22+
<p><strong>Constraints:</strong></p>
23+
24+
<ul>
25+
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
26+
</ul>

0412-fizz-buzz/solution.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Approach 3: Hash it
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def fizzBuzz(self, n: int) -> List[str]:
8+
ans = []
9+
10+
fizz_buzz_dict = {3: 'Fizz', 5: 'Buzz'}
11+
divisors = [3, 5]
12+
13+
for num in range(1, n + 1):
14+
num_ans_str = []
15+
16+
for key in divisors:
17+
if num % key == 0:
18+
num_ans_str.append(fizz_buzz_dict[key])
19+
20+
if not num_ans_str:
21+
num_ans_str.append(str(num))
22+
23+
ans.append(''.join(num_ans_str))
24+
25+
return ans
26+

0 commit comments

Comments
 (0)