Nav: << previous: 267.回文排列 II | next: 269.火星词典 >>


Description

tab: English
 
<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>
 
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,0,1]</span></p>
 
<p><strong>Output:</strong> <span class="example-io">2</span></p>
 
<p><strong>Explanation:</strong></p>
 
<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>
</div>
 
<p><strong class="example">Example 2:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,1]</span></p>
 
<p><strong>Output:</strong> <span class="example-io">2</span></p>
 
<p><strong>Explanation:</strong></p>
 
<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>
</div>
 
<p><strong class="example">Example 3:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [9,6,4,2,3,5,7,0,1]</span></p>
 
<p><strong>Output:</strong> <span class="example-io">8</span></p>
 
<p><strong>Explanation:</strong></p>
 
<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>
</div>
 
<div class="simple-translate-system-theme" id="simple-translate">
<div>
<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>
 
<div class="simple-translate-panel " style="width: 300px; height: 200px; top: 0px; left: 0px; font-size: 13px;">
<div class="simple-translate-result-wrapper" style="overflow: hidden;">
<div class="simple-translate-move" draggable="true">&nbsp;</div>
 
<div class="simple-translate-result-contents">
<p class="simple-translate-result" dir="auto">&nbsp;</p>
 
<p class="simple-translate-candidate" dir="auto">&nbsp;</p>
</div>
</div>
</div>
</div>
</div>
 
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
 
<ul>
	<li><code>n == nums.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= n</code></li>
	<li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li>
</ul>
 
<p>&nbsp;</p>
<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>
 
 
 
---
 
[submissions](https://leetcode.com/problems/missing-number/submissions/) | [solutions](https://leetcode.com/problems/missing-number/solutions/)
 
 
tab: 中文
 
<p>给定一个包含 <code>[0, n]</code>&nbsp;中&nbsp;<code>n</code>&nbsp;个数的数组 <code>nums</code> ,找出 <code>[0, n]</code> 这个范围内没有出现在数组中的那个数。</p>
 
<ul>
</ul>
 
<p>&nbsp;</p>
 
<p><strong>示例 1:</strong></p>
 
<div class="example-block">
<p><strong>输入:</strong>nums = [3,0,1]</p>
 
<p><strong>输出:</strong>2</p>
 
<p><b>解释:</b><code>n = 3</code>,因为有 3 个数字,所以所有的数字都在范围 <code>[0,3]</code> 内。2 是丢失的数字,因为它没有出现在 <code>nums</code> 中。</p>
</div>
 
<p><strong>示例 2:</strong></p>
 
<div class="example-block">
<p><strong>输入:</strong>nums = [0,1]</p>
 
<p><strong>输出:</strong>2</p>
 
<p><b>解释:</b><code>n = 2</code>,因为有 2 个数字,所以所有的数字都在范围 <code>[0,2]</code> 内。2 是丢失的数字,因为它没有出现在 <code>nums</code> 中。</p>
</div>
 
<p><strong>示例 3:</strong></p>
 
<div class="example-block">
<p><strong>输入:</strong>nums = [9,6,4,2,3,5,7,0,1]</p>
 
<p><strong>输出:</strong>8</p>
 
<p><b>解释:</b><code>n = 9</code>,因为有 9 个数字,所以所有的数字都在范围 <code>[0,9]</code> 内。8 是丢失的数字,因为它没有出现在 <code>nums</code> 中。</p>
</div>
 
<p><strong>提示:</strong></p>
 
<ul>
	<li><code>n == nums.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= n</code></li>
	<li><code>nums</code> 中的所有数字都 <strong>独一无二</strong></li>
</ul>
 
<p>&nbsp;</p>
 
<p><strong>进阶:</strong>你能否实现线性时间复杂度、仅使用额外常数空间的算法解决此问题?</p>
 
 
 
---
 
[提交记录](https://leetcode.cn/problems/missing-number/submissions/) | [题解](https://leetcode.cn/problems/missing-number/solution/)
 
 

Solutions & Notes

properties:
  note.updated:
    displayName: Last Updated
  note.relative_links:
    displayName: Related Links
  note.desc:
    displayName: Description
  note.grade:
    displayName: Rating
  note.program_language:
    displayName: Language
  note.time_complexity:
    displayName: TC
  note.space_complexity:
    displayName: SC
views:
  - type: table
    name: Solutions & Notes
    filters:
      and:
        - file.hasLink(this.file)
        - file.tags.containsAny("leetcode/solution", "leetcode/note")
    order:
      - file.name
      - desc
      - program_language
      - time_complexity
      - space_complexity
      - grade
      - relative_links
      - updated
    sort:
      - property: grade
        direction: ASC
      - property: time_complexity
        direction: ASC
      - property: program_language
        direction: ASC
    columnSize:
      file.name: 104
      note.space_complexity: 65
      note.grade: 126
 

Similar Problems

properties:
  note.lcTopics:
    displayName: Topics
  note.lcAcRate:
    displayName: AC Rate
  note.favorites:
    displayName: Favorites
  note.grade:
    displayName: Rating
  note.translatedTitle:
    displayName: Title (CN)
  note.lcDifficulty:
    displayName: Difficulty
views:
  - type: table
    name: Similar Problems
    filters:
      and:
        - file.hasLink(this.file)
        - similarQuestions.contains(this.file)
    order:
      - file.name
      - translatedTitle
      - lcTopics
      - lcDifficulty
      - lcAcRate
      - grade
      - favorites
    sort:
      - property: file.name
        direction: ASC
      - property: lcTopics
        direction: DESC
    columnSize:
      note.translatedTitle: 240
      note.lcTopics: 347
      note.lcAcRate: 75
      note.grade: 122