Skip to content

Commit 0414bd6

Browse files
committed
feat: update solutions to lc problem: No.0136
No.0136.Single Number
1 parent 88da1c4 commit 0414bd6

File tree

7 files changed

+94
-89
lines changed

7 files changed

+94
-89
lines changed

solution/0100-0199/0136.Single Number/README.md

+44-35
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,16 @@
2727

2828
<!-- 这里可写通用的实现逻辑 -->
2929

30-
异或运算求解。
30+
**方法一:位运算**
3131

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` 的长度。
3340

3441
<!-- tabs:start -->
3542

@@ -40,10 +47,7 @@
4047
```python
4148
class Solution:
4249
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)
4751
```
4852

4953
### **Java**
@@ -53,55 +57,60 @@ class Solution:
5357
```java
5458
class Solution {
5559
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;
5963
}
60-
return res;
64+
return ans;
6165
}
6266
}
6367
```
6468

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+
```
6676

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;
7686
}
77-
return res;
7887
};
7988
```
8089
8190
### **Go**
8291
8392
```go
84-
func singleNumber(nums []int) int {
85-
res := 0
93+
func singleNumber(nums []int) (ans int) {
8694
for _, v := range nums {
87-
res ^= v
95+
ans ^= v
8896
}
89-
return res
97+
return
9098
}
9199
```
92100

93-
### **C++**
101+
### **JavaScript**
94102

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;
104112
}
113+
return ans;
105114
};
106115
```
107116

solution/0100-0199/0136.Single Number/README_EN.md

+35-33
Original file line numberDiff line numberDiff line change
@@ -37,66 +37,68 @@
3737
```python
3838
class Solution:
3939
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)
4441
```
4542

4643
### **Java**
4744

4845
```java
4946
class Solution {
5047
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;
5451
}
55-
return res;
52+
return ans;
5653
}
5754
}
5855
```
5956

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+
```
6164

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;
7174
}
72-
return res;
7375
};
7476
```
7577
7678
### **Go**
7779
7880
```go
79-
func singleNumber(nums []int) int {
80-
res := 0
81+
func singleNumber(nums []int) (ans int) {
8182
for _, v := range nums {
82-
res ^= v
83+
ans ^= v
8384
}
84-
return res
85+
return
8586
}
8687
```
8788

88-
### **C++**
89+
### **JavaScript**
8990

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;
99100
}
101+
return ans;
100102
};
101103
```
102104

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
class Solution {
22
public:
33
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;
97
}
108
};
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
func singleNumber(nums []int) int {
2-
res := 0
1+
func singleNumber(nums []int) (ans int) {
32
for _, v := range nums {
4-
res ^= v
3+
ans ^= v
54
}
6-
return res
5+
return
76
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution {
22
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;
66
}
7-
return res;
7+
return ans;
88
}
99
}

solution/0100-0199/0136.Single Number/Solution.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* @return {number}
44
*/
55
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;
99
}
10-
return res;
10+
return ans;
1111
};
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
class Solution:
22
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)

0 commit comments

Comments
 (0)