Nav: << previous: 135.分发糖果 | next: 137.只出现一次的数字 II >>
Description
tab: English
<p>Given a <strong>non-empty</strong> array of integers <code>nums</code>, every element appears <em>twice</em> except for one. Find that single one.</p>
<p>You must implement a solution with a linear runtime complexity and use only constant extra space.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,2,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,1,2,1,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>
<li><code>-3 * 10<sup>4</sup> <= nums[i] <= 3 * 10<sup>4</sup></code></li>
<li>Each element in the array appears twice except for one element which appears only once.</li>
</ul>
> [!tip]- Hint 1
>
> Think about the XOR (^) operator's property.
---
[submissions](https://leetcode.com/problems/single-number/submissions/) | [solutions](https://leetcode.com/problems/single-number/solutions/)
tab: 中文
<p>给你一个 <strong>非空</strong> 整数数组 <code>nums</code> ,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。</p>
<p>你必须设计并实现线性时间复杂度的算法来解决此问题,且该算法只使用常量额外空间。</p>
<div class="original__bRMd">
<div>
<p> </p>
<p><strong class="example">示例 1 :</strong></p>
<div class="example-block">
<p><strong>输入:</strong>nums = [2,2,1]</p>
<p><strong>输出:</strong>1</p>
</div>
<p><strong class="example">示例 2 :</strong></p>
<div class="example-block">
<p><strong>输入:</strong>nums = [4,1,2,1,2]</p>
<p><strong>输出:</strong>4</p>
</div>
<p><strong class="example">示例 3 :</strong></p>
<div class="example-block">
<p><strong>输入:</strong>nums = [1]</p>
<p><strong>输出:</strong>1</p>
</div>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>
<li><code>-3 * 10<sup>4</sup> <= nums[i] <= 3 * 10<sup>4</sup></code></li>
<li>除了某个元素只出现一次以外,其余每个元素均出现两次。</li>
</ul>
</div>
</div>
> [!tip]- 提示 1
>
> Think about the XOR (^) operator's property.
---
[提交记录](https://leetcode.cn/problems/single-number/submissions/) | [题解](https://leetcode.cn/problems/single-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