Skip to content

Commit 89ab157

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.7 MB (67.76%)
1 parent 75697a8 commit 89ab157

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<p>In the town of Digitville, there was a list of numbers called <code>nums</code> containing integers from <code>0</code> to <code>n - 1</code>. Each number was supposed to appear <strong>exactly once</strong> in the list, however, <strong>two</strong> mischievous numbers sneaked in an <em>additional time</em>, making the list longer than usual.<!-- notionvc: c37cfb04-95eb-4273-85d5-3c52d0525b95 --></p>
2+
3+
<p>As the town detective, your task is to find these two sneaky numbers. Return an array of size <strong>two</strong> containing the two numbers (in <em>any order</em>), so peace can return to Digitville.<!-- notionvc: 345db5be-c788-4828-9836-eefed31c982f --></p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<div class="example-block">
9+
<p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0]</span></p>
10+
11+
<p><strong>Output:</strong> <span class="example-io">[0,1]</span></p>
12+
13+
<p><strong>Explanation:</strong></p>
14+
15+
<p>The numbers 0 and 1 each appear twice in the array.</p>
16+
</div>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
<div class="example-block">
21+
<p><strong>Input:</strong> <span class="example-io">nums = [0,3,2,1,3,2]</span></p>
22+
23+
<p><strong>Output:</strong> <span class="example-io">[2,3]</span></p>
24+
25+
<p><strong>Explanation: </strong></p>
26+
27+
<p>The numbers 2 and 3 each appear twice in the array.</p>
28+
</div>
29+
30+
<p><strong class="example">Example 3:</strong></p>
31+
32+
<div class="example-block">
33+
<p><strong>Input:</strong> <span class="example-io">nums = [7,1,5,4,3,4,6,0,9,5,8,2]</span></p>
34+
35+
<p><strong>Output:</strong> <span class="example-io">[4,5]</span></p>
36+
37+
<p><strong>Explanation: </strong></p>
38+
39+
<p>The numbers 4 and 5 each appear twice in the array.</p>
40+
</div>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li data-stringify-border="0" data-stringify-indent="1"><code>2 &lt;= n &lt;= 100</code></li>
47+
<li data-stringify-border="0" data-stringify-indent="1"><code>nums.length == n + 2</code></li>
48+
<li data-stringify-border="0" data-stringify-indent="1"><code data-stringify-type="code">0 &lt;= nums[i] &lt; n</code></li>
49+
<li data-stringify-border="0" data-stringify-indent="1">The input is generated such that <code>nums</code> contains <strong>exactly</strong> two repeated elements.</li>
50+
</ul>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Approach 1: Hash Table
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def getSneakyNumbers(self, nums: List[int]) -> List[int]:
8+
res = []
9+
count = {}
10+
11+
for x in nums:
12+
count[x] = count.get(x, 0) + 1
13+
if count[x] == 2:
14+
res.append(x)
15+
16+
return res
17+

0 commit comments

Comments
 (0)