|
| 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> </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("moz-extension://8a9ffb6b-7e69-4e93-aae1-436a1448eff6/icons/512.png"); height: 22px; width: 22px; top: 318px; left: 36px;"> </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"> </div> |
| 47 | + |
| 48 | +<div class="simple-translate-result-contents"> |
| 49 | +<p class="simple-translate-result" dir="auto"> </p> |
| 50 | + |
| 51 | +<p class="simple-translate-candidate" dir="auto"> </p> |
| 52 | +</div> |
| 53 | +</div> |
| 54 | +</div> |
| 55 | +</div> |
| 56 | +</div> |
| 57 | + |
| 58 | +<p> </p> |
| 59 | +<p><strong>Constraints:</strong></p> |
| 60 | + |
| 61 | +<ul> |
| 62 | + <li><code>n == nums.length</code></li> |
| 63 | + <li><code>1 <= n <= 10<sup>4</sup></code></li> |
| 64 | + <li><code>0 <= nums[i] <= n</code></li> |
| 65 | + <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> |
| 66 | +</ul> |
| 67 | + |
| 68 | +<p> </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> |
0 commit comments