48
48
49
49
<!-- 这里可写通用的实现逻辑 -->
50
50
51
+ ** 方法一:位运算**
52
+
53
+ 异或运算有以下性质:
54
+
55
+ - 任何数和 $0$ 做异或运算,结果仍然是原来的数,即 $x \oplus 0 = x$;
56
+ - 任何数和其自身做异或运算,结果是 $0$,即 $x \oplus x = 0$;
57
+
58
+ 由于数组中除了两个数字之外,其他数字都出现了两次,因此我们对数组中的所有数字进行异或运算,得到的结果即为两个只出现一次的数字的异或结果。
59
+
60
+ 而由于这两个数字不相等,因此异或结果中至少存在一位为 $1$。我们可以通过 ` lowbit ` 运算找到异或结果中最低位的 $1$,并将数组中的所有数字按照该位是否为 $1$ 分为两组,这样两个只出现一次的数字就被分到了不同的组中。
61
+
62
+ 对两个组分别进行异或运算,即可得到两个只出现一次的数字 $a$ 和 $b$。
63
+
64
+ 时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。
65
+
51
66
<!-- tabs:start -->
52
67
53
68
### ** Python3**
57
72
``` python
58
73
class Solution :
59
74
def singleNumber (self , nums : List[int ]) -> List[int ]:
60
- eor = 0
61
- for x in nums:
62
- eor ^= x
63
- lowbit = eor & (- eor)
64
- ans = [0 , 0 ]
75
+ xs = reduce (xor, nums)
76
+ a = 0
77
+ lb = xs & - xs
65
78
for x in nums:
66
- if ( x & lowbit) == 0 :
67
- ans[ 0 ] ^= x
68
- ans[ 1 ] = eor ^ ans[ 0 ]
69
- return ans
79
+ if x & lb :
80
+ a ^= x
81
+ b = xs ^ a
82
+ return [a, b]
70
83
```
71
84
72
85
### ** Java**
@@ -76,61 +89,42 @@ class Solution:
76
89
``` java
77
90
class Solution {
78
91
public int [] singleNumber (int [] nums ) {
79
- int eor = 0 ;
92
+ int xs = 0 ;
80
93
for (int x : nums) {
81
- eor ^ = x;
94
+ xs ^ = x;
82
95
}
83
- int lowbit = eor & ( - eor) ;
84
- int [] ans = new int [ 2 ] ;
96
+ int lb = xs & - xs ;
97
+ int a = 0 ;
85
98
for (int x : nums) {
86
- if ((x & lowbit) = = 0 ) {
87
- ans[ 0 ] ^ = x;
99
+ if ((x & lb) ! = 0 ) {
100
+ a ^ = x;
88
101
}
89
102
}
90
- ans[ 1 ] = eor ^ ans[ 0 ] ;
91
- return ans ;
103
+ int b = xs ^ a ;
104
+ return new int [] {a, b} ;
92
105
}
93
106
}
94
107
```
95
108
96
- ### ** JavaScript**
97
-
98
- ``` js
99
- /**
100
- * @param {number[]} nums
101
- * @return {number[]}
102
- */
103
- var singleNumber = function (nums ) {
104
- let eor = 0 ;
105
- for (const x of nums) {
106
- eor ^= x;
107
- }
108
- const lowbit = eor & - eor;
109
- let ans = [0 ];
110
- for (const x of nums) {
111
- if ((x & lowbit) == 0 ) {
112
- ans[0 ] ^= x;
113
- }
114
- }
115
- ans .push (eor ^ ans[0 ]);
116
- return ans;
117
- };
118
- ```
119
-
120
109
### ** C++**
121
110
122
111
``` cpp
123
112
class Solution {
124
113
public:
125
114
vector<int > singleNumber(vector<int >& nums) {
126
- long long eor = 0;
127
- for (int x : nums) eor ^= x;
128
- int lowbit = eor & (-eor);
129
- vector<int > ans(2);
130
- for (int x : nums)
131
- if ((x & lowbit) == 0) ans[ 0] ^= x;
132
- ans[ 1] = eor ^ ans[ 0] ;
133
- return ans;
115
+ long long xs = 0;
116
+ for (int& x : nums) {
117
+ xs ^= x;
118
+ }
119
+ int lb = xs & -xs;
120
+ int a = 0;
121
+ for (int& x : nums) {
122
+ if (x & lb) {
123
+ a ^= x;
124
+ }
125
+ }
126
+ int b = xs ^ a;
127
+ return {a, b};
134
128
}
135
129
};
136
130
```
@@ -139,22 +133,98 @@ public:
139
133
140
134
```go
141
135
func singleNumber(nums []int) []int {
142
- eor := 0
136
+ xs := 0
143
137
for _, x := range nums {
144
- eor ^= x
138
+ xs ^= x
145
139
}
146
- lowbit := eor & (-eor)
147
- ans := make([]int, 2)
140
+ lb := xs & -xs
141
+ a := 0
148
142
for _, x := range nums {
149
- if (x & lowbit) = = 0 {
150
- ans[0] ^= x
143
+ if x&lb ! = 0 {
144
+ a ^= x
151
145
}
152
146
}
153
- ans[1] = eor ^ ans[0]
154
- return ans
147
+ b := xs ^ a
148
+ return []int{a, b}
149
+ }
150
+ ```
151
+
152
+ ### ** Rust**
153
+
154
+ ``` rust
155
+ impl Solution {
156
+ pub fn single_number (nums : Vec <i32 >) -> Vec <i32 > {
157
+ let xs = nums . iter (). fold (0 , | r , v | r ^ v );
158
+ let lb = xs & - xs ;
159
+ let mut a = 0 ;
160
+ for x in & nums {
161
+ if x & lb != 0 {
162
+ a ^= x ;
163
+ }
164
+ }
165
+ let b = xs ^ a ;
166
+ vec! [a , b ]
167
+ }
168
+ }
169
+ ```
170
+
171
+ ### ** TypeScript**
172
+
173
+ ``` ts
174
+ function singleNumber(nums : number []): number [] {
175
+ const xs = nums .reduce ((a , b ) => a ^ b );
176
+ const lb = xs & - xs ;
177
+ let a = 0 ;
178
+ for (const x of nums ) {
179
+ if (x & lb ) {
180
+ a ^= x ;
181
+ }
182
+ }
183
+ const b = xs ^ a ;
184
+ return [a , b ];
185
+ }
186
+ ```
187
+
188
+ ### ** C#**
189
+
190
+ ``` cs
191
+ public class Solution {
192
+ public int [] SingleNumber (int [] nums ) {
193
+ int xs = nums .Aggregate (0 , (a , b ) => a ^ b );
194
+ int lb = xs & - xs ;
195
+ int a = 0 ;
196
+ foreach (int x in nums ) {
197
+ if ((x & lb ) != 0 ) {
198
+ a ^= x ;
199
+ }
200
+ }
201
+ int b = xs ^ a ;
202
+ return new int [] {a , b };
203
+ }
155
204
}
156
205
```
157
206
207
+ ### ** JavaScript**
208
+
209
+ ``` js
210
+ /**
211
+ * @param {number[]} nums
212
+ * @return {number[]}
213
+ */
214
+ var singleNumber = function (nums ) {
215
+ const xs = nums .reduce ((a , b ) => a ^ b);
216
+ const lb = xs & - xs;
217
+ let a = 0 ;
218
+ for (const x of nums) {
219
+ if (x & lb) {
220
+ a ^= x;
221
+ }
222
+ }
223
+ const b = xs ^ a;
224
+ return [a, b];
225
+ };
226
+ ```
227
+
158
228
### ** ...**
159
229
160
230
```
0 commit comments