|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/0100-0199/0136.Single%20Number/README_EN.md |
| 5 | +tags: |
| 6 | + - Bit Manipulation |
| 7 | + - Array |
| 8 | +--- |
| 9 | + |
| 10 | +<!-- problem:start --> |
| 11 | + |
| 12 | +# [136. Single Number](https://leetcode.com/problems/single-number) |
| 13 | + |
| 14 | +[中文文档](/solution/0100-0199/0136.Single%20Number/README.md) |
| 15 | + |
| 16 | +## Description |
| 17 | + |
| 18 | +<!-- description:start --> |
| 19 | + |
| 20 | +<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> |
| 21 | + |
| 22 | +<p>You must implement a solution with a linear runtime complexity and use only constant extra space.</p> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | +<p><strong class="example">Example 1:</strong></p> |
| 26 | + |
| 27 | +<div class="example-block"> |
| 28 | +<p><strong>Input:</strong> <span class="example-io">nums = [2,2,1]</span></p> |
| 29 | + |
| 30 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 31 | +</div> |
| 32 | + |
| 33 | +<p><strong class="example">Example 2:</strong></p> |
| 34 | + |
| 35 | +<div class="example-block"> |
| 36 | +<p><strong>Input:</strong> <span class="example-io">nums = [4,1,2,1,2]</span></p> |
| 37 | + |
| 38 | +<p><strong>Output:</strong> <span class="example-io">4</span></p> |
| 39 | +</div> |
| 40 | + |
| 41 | +<p><strong class="example">Example 3:</strong></p> |
| 42 | + |
| 43 | +<div class="example-block"> |
| 44 | +<p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> |
| 45 | + |
| 46 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 47 | +</div> |
| 48 | + |
| 49 | +<p> </p> |
| 50 | +<p><strong>Constraints:</strong></p> |
| 51 | + |
| 52 | +<ul> |
| 53 | + <li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li> |
| 54 | + <li><code>-3 * 10<sup>4</sup> <= nums[i] <= 3 * 10<sup>4</sup></code></li> |
| 55 | + <li>Each element in the array appears twice except for one element which appears only once.</li> |
| 56 | +</ul> |
| 57 | + |
| 58 | +<!-- description:end --> |
| 59 | + |
| 60 | +## Solutions |
| 61 | + |
| 62 | +<!-- solution:start --> |
| 63 | + |
| 64 | +### Solution 1: Bitwise Operation |
| 65 | + |
| 66 | +The XOR operation has the following properties: |
| 67 | + |
| 68 | +- Any number XOR 0 is still the original number, i.e., $x \oplus 0 = x$; |
| 69 | +- Any number XOR itself is 0, i.e., $x \oplus x = 0$; |
| 70 | + |
| 71 | +Performing XOR operation on all elements in the array will result in the number that only appears once. |
| 72 | + |
| 73 | +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. |
| 74 | + |
| 75 | +<!-- tabs:start --> |
| 76 | + |
| 77 | +#### Python3 |
| 78 | + |
| 79 | +```python |
| 80 | +class Solution: |
| 81 | + def singleNumber(self, nums: List[int]) -> int: |
| 82 | + return reduce(xor, nums) |
| 83 | +``` |
| 84 | + |
| 85 | +#### Java |
| 86 | + |
| 87 | +```java |
| 88 | +class Solution { |
| 89 | + public int singleNumber(int[] nums) { |
| 90 | + int ans = 0; |
| 91 | + for (int v : nums) { |
| 92 | + ans ^= v; |
| 93 | + } |
| 94 | + return ans; |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +#### C++ |
| 100 | + |
| 101 | +```cpp |
| 102 | +class Solution { |
| 103 | +public: |
| 104 | + int singleNumber(vector<int>& nums) { |
| 105 | + int ans = 0; |
| 106 | + for (int v : nums) { |
| 107 | + ans ^= v; |
| 108 | + } |
| 109 | + return ans; |
| 110 | + } |
| 111 | +}; |
| 112 | +``` |
| 113 | +
|
| 114 | +
|
| 115 | +<!-- tabs:end --> |
| 116 | +
|
| 117 | +<!-- solution:end --> |
| 118 | +
|
| 119 | +<!-- problem:end --> |
0 commit comments