Skip to content

Commit 8307a79

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 18.5 MB (77.72%)
1 parent 0f0492a commit 8307a79

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

0268-missing-number/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
6+
<div class="example-block">
7+
<p><strong>Input:</strong> <span class="example-io">nums = [3,0,1]</span></p>
8+
9+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
10+
11+
<p><strong>Explanation:</strong></p>
12+
13+
<p><code>n = 3</code> since there are 3 numbers, so all numbers are in the range <code>[0,3]</code>. 2 is the missing number in the range since it does not appear in <code>nums</code>.</p>
14+
</div>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<div class="example-block">
19+
<p><strong>Input:</strong> <span class="example-io">nums = [0,1]</span></p>
20+
21+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
22+
23+
<p><strong>Explanation:</strong></p>
24+
25+
<p><code>n = 2</code> since there are 2 numbers, so all numbers are in the range <code>[0,2]</code>. 2 is the missing number in the range since it does not appear in <code>nums</code>.</p>
26+
</div>
27+
28+
<p><strong class="example">Example 3:</strong></p>
29+
30+
<div class="example-block">
31+
<p><strong>Input:</strong> <span class="example-io">nums = [9,6,4,2,3,5,7,0,1]</span></p>
32+
33+
<p><strong>Output:</strong> <span class="example-io">8</span></p>
34+
35+
<p><strong>Explanation:</strong></p>
36+
37+
<p><code>n = 9</code> since there are 9 numbers, so all numbers are in the range <code>[0,9]</code>. 8 is the missing number in the range since it does not appear in <code>nums</code>.</p>
38+
</div>
39+
40+
<div class="simple-translate-system-theme" id="simple-translate">
41+
<div>
42+
<div class="simple-translate-button isShow" style="background-image: url(&quot;moz-extension://8a9ffb6b-7e69-4e93-aae1-436a1448eff6/icons/512.png&quot;); height: 22px; width: 22px; top: 318px; left: 36px;">&nbsp;</div>
43+
44+
<div class="simple-translate-panel " style="width: 300px; height: 200px; top: 0px; left: 0px; font-size: 13px;">
45+
<div class="simple-translate-result-wrapper" style="overflow: hidden;">
46+
<div class="simple-translate-move" draggable="true">&nbsp;</div>
47+
48+
<div class="simple-translate-result-contents">
49+
<p class="simple-translate-result" dir="auto">&nbsp;</p>
50+
51+
<p class="simple-translate-candidate" dir="auto">&nbsp;</p>
52+
</div>
53+
</div>
54+
</div>
55+
</div>
56+
</div>
57+
58+
<p>&nbsp;</p>
59+
<p><strong>Constraints:</strong></p>
60+
61+
<ul>
62+
<li><code>n == nums.length</code></li>
63+
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
64+
<li><code>0 &lt;= nums[i] &lt;= n</code></li>
65+
<li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li>
66+
</ul>
67+
68+
<p>&nbsp;</p>
69+
<p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>

0268-missing-number/solution.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Approach 4: Math
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def missingNumber(self, nums: List[int]) -> int:
8+
n = len(nums)
9+
expected_sum = n * (n + 1) // 2
10+
actual_sum = sum(nums)
11+
return expected_sum - actual_sum

0 commit comments

Comments
 (0)