Nav: << previous: 286.墙与门 | next: 288.单词的唯一缩写 >>
Description
tab: English
<p>Given an array of integers <code>nums</code> containing <code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p>
<p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this repeated number</em>.</p>
<p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code> and using only constant extra space.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,4,2,2]
<strong>Output:</strong> 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,4,2]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,3,3,3,3]
<strong>Output:</strong> 3</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>nums.length == n + 1</code></li>
<li><code>1 <= nums[i] <= n</code></li>
<li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li>
</ul>
<p> </p>
<p><b>Follow up:</b></p>
<ul>
<li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li>
<li>Can you solve the problem in linear runtime complexity?</li>
</ul>
---
[submissions](https://leetcode.com/problems/find-the-duplicate-number/submissions/) | [solutions](https://leetcode.com/problems/find-the-duplicate-number/solutions/)
tab: 中文
<p>给定一个包含 <code>n + 1</code> 个整数的数组 <code>nums</code> ,其数字都在 <code>[1, n]</code> 范围内(包括 <code>1</code> 和 <code>n</code>),可知至少存在一个重复的整数。</p>
<p>假设 <code>nums</code> 只有 <strong>一个重复的整数</strong> ,返回 <strong>这个重复的数</strong> 。</p>
<p>你设计的解决方案必须 <strong>不修改</strong> 数组 <code>nums</code> 且只用常量级 <code>O(1)</code> 的额外空间。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>nums = [1,3,4,2,2]
<strong>输出:</strong>2
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>nums = [3,1,3,4,2]
<strong>输出:</strong>3
</pre>
<p><strong>示例 3 :</strong></p>
<pre>
<strong>输入:</strong>nums = [3,3,3,3,3]
<strong>输出:</strong>3
</pre>
<p> </p>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>nums.length == n + 1</code></li>
<li><code>1 <= nums[i] <= n</code></li>
<li><code>nums</code> 中 <strong>只有一个整数</strong> 出现 <strong>两次或多次</strong> ,其余整数均只出现 <strong>一次</strong></li>
</ul>
<p> </p>
<p><b>进阶:</b></p>
<ul>
<li>如何证明 <code>nums</code> 中至少存在一个重复的数字?</li>
<li>你可以设计一个线性级时间复杂度 <code>O(n)</code> 的解决方案吗?</li>
</ul>
---
[提交记录](https://leetcode.cn/problems/find-the-duplicate-number/submissions/) | [题解](https://leetcode.cn/problems/find-the-duplicate-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