File tree 7 files changed +94
-89
lines changed
solution/0100-0199/0136.Single Number
7 files changed +94
-89
lines changed Original file line number Diff line number Diff line change 27
27
28
28
<!-- 这里可写通用的实现逻辑 -->
29
29
30
- 异或运算求解。
30
+ ** 方法一:位运算 **
31
31
32
- 首先明确,两个相同的数异或之后的结果为 0。对该数组所有元素进行异或运算,结果就是那个只出现一次的数字。
32
+ 异或运算的性质:
33
+
34
+ - 任何数和 $0$ 做异或运算,结果仍然是原来的数,即 $x \oplus 0 = x$;
35
+ - 任何数和其自身做异或运算,结果是 $0$,即 $x \oplus x = 0$;
36
+
37
+ 我们对该数组所有元素进行异或运算,结果就是那个只出现一次的数字。
38
+
39
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是数组 ` nums ` 的长度。
33
40
34
41
<!-- tabs:start -->
35
42
40
47
``` python
41
48
class Solution :
42
49
def singleNumber (self , nums : List[int ]) -> int :
43
- res = 0
44
- for num in nums:
45
- res ^= num
46
- return res
50
+ return reduce (xor, nums)
47
51
```
48
52
49
53
### ** Java**
@@ -53,55 +57,60 @@ class Solution:
53
57
``` java
54
58
class Solution {
55
59
public int singleNumber (int [] nums ) {
56
- int res = 0 ;
57
- for (int num : nums) {
58
- res ^ = num ;
60
+ int ans = 0 ;
61
+ for (int v : nums) {
62
+ ans ^ = v ;
59
63
}
60
- return res ;
64
+ return ans ;
61
65
}
62
66
}
63
67
```
64
68
65
- ### ** JavaScript**
69
+ ``` java
70
+ class Solution {
71
+ public int singleNumber (int [] nums ) {
72
+ return Arrays . stream(nums). reduce(0 , (a, b) - > a ^ b);
73
+ }
74
+ }
75
+ ```
66
76
67
- ``` js
68
- /**
69
- * @param {number[]} nums
70
- * @return {number}
71
- */
72
- var singleNumber = function ( nums ) {
73
- let res = 0 ;
74
- for (let num of nums) {
75
- res ^= num ;
77
+ ### ** C++ **
78
+
79
+ ``` cpp
80
+ class Solution {
81
+ public:
82
+ int singleNumber(vector< int >& nums) {
83
+ int ans = 0;
84
+ for (int v : nums) ans ^= v;
85
+ return ans ;
76
86
}
77
- return res;
78
87
};
79
88
```
80
89
81
90
### **Go**
82
91
83
92
```go
84
- func singleNumber (nums []int ) int {
85
- res := 0
93
+ func singleNumber(nums []int) (ans int) {
86
94
for _, v := range nums {
87
- res ^= v
95
+ ans ^= v
88
96
}
89
- return res
97
+ return
90
98
}
91
99
```
92
100
93
- ### ** C++ **
101
+ ### ** JavaScript **
94
102
95
- ``` cpp
96
- class Solution {
97
- public:
98
- int singleNumber(vector< int >& nums) {
99
- int res = 0;
100
- for (auto num : nums) {
101
- res ^= num ;
102
- }
103
- return res ;
103
+ ``` js
104
+ /**
105
+ * @param {number[]} nums
106
+ * @return {number}
107
+ */
108
+ var singleNumber = function ( nums ) {
109
+ let ans = 0 ;
110
+ for ( const v of nums) {
111
+ ans ^= v ;
104
112
}
113
+ return ans;
105
114
};
106
115
```
107
116
Original file line number Diff line number Diff line change 37
37
``` python
38
38
class Solution :
39
39
def singleNumber (self , nums : List[int ]) -> int :
40
- res = 0
41
- for num in nums:
42
- res ^= num
43
- return res
40
+ return reduce (xor, nums)
44
41
```
45
42
46
43
### ** Java**
47
44
48
45
``` java
49
46
class Solution {
50
47
public int singleNumber (int [] nums ) {
51
- int res = 0 ;
52
- for (int num : nums) {
53
- res ^ = num ;
48
+ int ans = 0 ;
49
+ for (int v : nums) {
50
+ ans ^ = v ;
54
51
}
55
- return res ;
52
+ return ans ;
56
53
}
57
54
}
58
55
```
59
56
60
- ### ** JavaScript**
57
+ ``` java
58
+ class Solution {
59
+ public int singleNumber (int [] nums ) {
60
+ return Arrays . stream(nums). reduce(0 , (a, b) - > a ^ b);
61
+ }
62
+ }
63
+ ```
61
64
62
- ``` js
63
- /**
64
- * @param {number[]} nums
65
- * @return {number}
66
- */
67
- var singleNumber = function ( nums ) {
68
- let res = 0 ;
69
- for (let num of nums) {
70
- res ^= num ;
65
+ ### ** C++ **
66
+
67
+ ``` cpp
68
+ class Solution {
69
+ public:
70
+ int singleNumber(vector< int >& nums) {
71
+ int ans = 0;
72
+ for (int v : nums) ans ^= v;
73
+ return ans ;
71
74
}
72
- return res;
73
75
};
74
76
```
75
77
76
78
### **Go**
77
79
78
80
```go
79
- func singleNumber (nums []int ) int {
80
- res := 0
81
+ func singleNumber(nums []int) (ans int) {
81
82
for _, v := range nums {
82
- res ^= v
83
+ ans ^= v
83
84
}
84
- return res
85
+ return
85
86
}
86
87
```
87
88
88
- ### ** C++ **
89
+ ### ** JavaScript **
89
90
90
- ``` cpp
91
- class Solution {
92
- public:
93
- int singleNumber(vector< int >& nums) {
94
- int res = 0;
95
- for (auto num : nums) {
96
- res ^= num ;
97
- }
98
- return res ;
91
+ ``` js
92
+ /**
93
+ * @param {number[]} nums
94
+ * @return {number}
95
+ */
96
+ var singleNumber = function ( nums ) {
97
+ let ans = 0 ;
98
+ for ( const v of nums) {
99
+ ans ^= v ;
99
100
}
101
+ return ans;
100
102
};
101
103
```
102
104
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
int singleNumber (vector<int >& nums) {
4
- int res = 0 ;
5
- for (auto num : nums) {
6
- res ^= num;
7
- }
8
- return res;
4
+ int ans = 0 ;
5
+ for (int v : nums) ans ^= v;
6
+ return ans;
9
7
}
10
8
};
Original file line number Diff line number Diff line change 1
- func singleNumber (nums []int ) int {
2
- res := 0
1
+ func singleNumber (nums []int ) (ans int ) {
3
2
for _ , v := range nums {
4
- res ^= v
3
+ ans ^= v
5
4
}
6
- return res
5
+ return
7
6
}
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int singleNumber (int [] nums ) {
3
- int res = 0 ;
4
- for (int num : nums ) {
5
- res ^= num ;
3
+ int ans = 0 ;
4
+ for (int v : nums ) {
5
+ ans ^= v ;
6
6
}
7
- return res ;
7
+ return ans ;
8
8
}
9
9
}
Original file line number Diff line number Diff line change 3
3
* @return {number }
4
4
*/
5
5
var singleNumber = function ( nums ) {
6
- let res = 0 ;
7
- for ( let num of nums ) {
8
- res ^= num ;
6
+ let ans = 0 ;
7
+ for ( const v of nums ) {
8
+ ans ^= v ;
9
9
}
10
- return res ;
10
+ return ans ;
11
11
} ;
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def singleNumber (self , nums : List [int ]) -> int :
3
- res = 0
4
- for num in nums :
5
- res ^= num
6
- return res
3
+ return reduce (xor , nums )
You can’t perform that action at this time.
0 commit comments