4848
4949<!-- 这里可写通用的实现逻辑 -->
5050
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+ 5166<!-- tabs:start -->
5267
5368### ** Python3**
5772``` python
5873class Solution :
5974 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
6578 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]
7083```
7184
7285### ** Java**
@@ -76,61 +89,42 @@ class Solution:
7689``` java
7790class Solution {
7891 public int [] singleNumber (int [] nums ) {
79- int eor = 0 ;
92+ int xs = 0 ;
8093 for (int x : nums) {
81- eor ^ = x;
94+ xs ^ = x;
8295 }
83- int lowbit = eor & ( - eor) ;
84- int [] ans = new int [ 2 ] ;
96+ int lb = xs & - xs ;
97+ int a = 0 ;
8598 for (int x : nums) {
86- if ((x & lowbit) = = 0 ) {
87- ans[ 0 ] ^ = x;
99+ if ((x & lb) ! = 0 ) {
100+ a ^ = x;
88101 }
89102 }
90- ans[ 1 ] = eor ^ ans[ 0 ] ;
91- return ans ;
103+ int b = xs ^ a ;
104+ return new int [] {a, b} ;
92105 }
93106}
94107```
95108
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- 120109### ** C++**
121110
122111``` cpp
123112class Solution {
124113public:
125114 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};
134128 }
135129};
136130```
@@ -139,22 +133,98 @@ public:
139133
140134```go
141135func singleNumber(nums []int) []int {
142- eor := 0
136+ xs := 0
143137 for _, x := range nums {
144- eor ^= x
138+ xs ^= x
145139 }
146- lowbit := eor & (-eor)
147- ans := make([]int, 2)
140+ lb := xs & -xs
141+ a := 0
148142 for _, x := range nums {
149- if (x & lowbit) = = 0 {
150- ans[0] ^= x
143+ if x&lb ! = 0 {
144+ a ^= x
151145 }
152146 }
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+ }
155204}
156205```
157206
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+ 158228### ** ...**
159229
160230```
0 commit comments