Skip to content

Commit 219fee9

Browse files
add question no 260
1 parent fae4e9d commit 219fee9

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
---
2+
id: single-number-III
3+
title: Single Number III
4+
sidebar_label: 0260-Single-Number-III
5+
tags:
6+
- Array
7+
- Bit Manipulation
8+
- C++
9+
- Java
10+
- Python
11+
description: "Given an integer array `nums`, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order."
12+
---
13+
14+
## Problem
15+
16+
Given an integer array `nums`, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order.
17+
18+
You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.
19+
20+
### Examples
21+
22+
**Example 1:**
23+
```
24+
Input: nums = [1,2,1,3,2,5]
25+
Output: [3,5]
26+
Explanation: [5, 3] is also a valid answer.
27+
```
28+
**Example 2:**
29+
30+
Input: nums = [-1,0]
31+
Output: [-1,0]
32+
33+
**Example 3:**
34+
35+
Input: nums = [0,1]
36+
Output: [1,0]
37+
38+
39+
### Constraints
40+
41+
- `2 <= nums.length <= 3 * 10^4`
42+
- `-2^31 <= nums[i] <= 2^31 - 1`
43+
- Each integer in `nums` will appear twice, only two integers will appear once.
44+
45+
### Approach
46+
47+
Once again, we need to use XOR to solve this problem. But this time, we need to do it in two passes:
48+
49+
- In the first pass, we XOR all elements in the array, and get the XOR of the two numbers we need to find. Note that since the two numbers are distinct, so there must be a set bit (that is, `the bit with value '1'`) in the XOR result. Find
50+
out an arbitrary set bit (for example, the rightmost set bit).
51+
52+
- In the second pass, we divide all numbers into two groups, one with the aforementioned bit set, another with the aforementinoed bit unset. Two different numbers we need to find must fall into thte two distrinct groups. XOR numbers in each group, we can find a number in either group.
53+
54+
### Solution
55+
56+
#### Code in Different Languages
57+
58+
### C++ Solution
59+
```cpp
60+
class Solution
61+
{
62+
public:
63+
vector<int> singleNumber(vector<int>& nums)
64+
{
65+
// Pass 1 :
66+
// Get the XOR of the two numbers we need to find
67+
int diff = accumulate(nums.begin(), nums.end(), 0, bit_xor<int>());
68+
// Get its last set bit
69+
diff &= -diff;
70+
71+
// Pass 2 :
72+
vector<int> rets = {0, 0}; // this vector stores the two numbers we will return
73+
for (int num : nums)
74+
{
75+
if ((num & diff) == 0) // the bit is not set
76+
{
77+
rets[0] ^= num;
78+
}
79+
else // the bit is set
80+
{
81+
rets[1] ^= num;
82+
}
83+
}
84+
return rets;
85+
}
86+
};
87+
```
88+
### Java Solution
89+
90+
```java
91+
public class Solution {
92+
public int[] singleNumber(int[] nums) {
93+
// Pass 1 :
94+
// Get the XOR of the two numbers we need to find
95+
int diff = 0;
96+
for (int num : nums) {
97+
diff ^= num;
98+
}
99+
// Get its last set bit
100+
diff &= -diff;
101+
102+
// Pass 2 :
103+
int[] rets = {0, 0}; // this array stores the two numbers we will return
104+
for (int num : nums)
105+
{
106+
if ((num & diff) == 0) // the bit is not set
107+
{
108+
rets[0] ^= num;
109+
}
110+
else // the bit is set
111+
{
112+
rets[1] ^= num;
113+
}
114+
}
115+
return rets;
116+
}
117+
}
118+
```
119+
### Python Solution
120+
121+
```python
122+
class Solution:
123+
def singleNumber(self, nums):
124+
s = reduce(xor, nums)
125+
nz = s & (s-1) ^ s
126+
num1 = reduce(xor, filter(lambda n: n & nz, nums))
127+
return(num1, s ^ num1)
128+
129+
```
130+
### Complexity Analysis
131+
**Time Complexity:** O(N)
132+
133+
**Space Complexity:** O(1)
134+
135+
### References
136+
**LeetCode Problem:** Single Number III

0 commit comments

Comments
 (0)