Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 862e62a

Browse files
committed
feat: add solutions to lc problem: No.0260.Single Number III
1 parent e96084b commit 862e62a

File tree

7 files changed

+225
-58
lines changed

7 files changed

+225
-58
lines changed

‎solution/0200-0299/0260.Single Number III/README.md‎

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,15 @@
5959
class Solution:
6060
def singleNumber(self, nums: List[int]) -> List[int]:
6161
eor = 0
62-
for num in nums:
63-
eor ^= num
64-
# 提取最右边的 1
65-
diff = eor & (~eor + 1)
66-
a = 0
67-
for num in nums:
68-
if (num & diff) == 0:
69-
a ^= num
70-
b = eor ^ a
71-
return [a, b]
62+
for x in nums:
63+
eor ^= x
64+
lowbit = eor & (-eor)
65+
ans = [0, 0]
66+
for x in nums:
67+
if (x & lowbit) == 0:
68+
ans[0] ^= x
69+
ans[1] = eor ^ ans[0]
70+
return ans
7271
```
7372

7473
### **Java**
@@ -79,23 +78,84 @@ class Solution:
7978
class Solution {
8079
public int[] singleNumber(int[] nums) {
8180
int eor = 0;
82-
for (int num : nums) {
83-
eor ^= num;
81+
for (int x : nums) {
82+
eor ^= x;
8483
}
85-
// 提取最右的 1
86-
int diff = eor & (~eor + 1);
87-
int a = 0;
88-
for (int num : nums) {
89-
if ((num & diff) == 0) {
90-
a ^= num;
84+
int lowbit = eor & (-eor);
85+
int[] ans = new int[2];
86+
for (int x : nums) {
87+
if ((x & lowbit) == 0) {
88+
ans[0] ^= x;
9189
}
9290
}
93-
int b = eor ^ a;
94-
return newint[]{a, b};
91+
ans[1] = eor ^ ans[0];
92+
return ans;
9593
}
9694
}
9795
```
9896

97+
### **JavaScript**
98+
99+
```js
100+
/**
101+
* @param {number[]} nums
102+
* @return {number[]}
103+
*/
104+
var singleNumber = function(nums) {
105+
let eor = 0;
106+
for (const x of nums) {
107+
eor ^= x;
108+
}
109+
const lowbit = eor & (-eor);
110+
let ans = [0];
111+
for (const x of nums) {
112+
if ((x & lowbit) == 0) {
113+
ans[0] ^= x;
114+
}
115+
}
116+
ans.push(eor ^ ans[0]);
117+
return ans;
118+
};
119+
```
120+
121+
### **C++**
122+
123+
```cpp
124+
class Solution {
125+
public:
126+
vector<int> singleNumber(vector<int>& nums) {
127+
long long eor = 0;
128+
for (int x : nums) eor ^= x;
129+
int lowbit = eor & (-eor);
130+
vector<int> ans(2);
131+
for (int x : nums)
132+
if ((x & lowbit) == 0) ans[0] ^= x;
133+
ans[1] = eor ^ ans[0];
134+
return ans;
135+
}
136+
};
137+
```
138+
139+
### **Go**
140+
141+
```go
142+
func singleNumber(nums []int) []int {
143+
eor := 0
144+
for _, x := range nums {
145+
eor ^= x
146+
}
147+
lowbit := eor & (-eor)
148+
ans := make([]int, 2)
149+
for _, x := range nums {
150+
if (x & lowbit) == 0 {
151+
ans[0] ^= x
152+
}
153+
}
154+
ans[1] = eor ^ ans[0]
155+
return ans
156+
}
157+
```
158+
99159
### **...**
100160

101161
```

‎solution/0200-0299/0260.Single Number III/README_EN.md‎

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@
5151
class Solution:
5252
def singleNumber(self, nums: List[int]) -> List[int]:
5353
eor = 0
54-
for num in nums:
55-
eor ^= num
56-
diff = eor & (~eor+1)
57-
a = 0
58-
for num in nums:
59-
if (num & diff) == 0:
60-
a ^= num
61-
b = eor ^ a
62-
return [a, b]
54+
for x in nums:
55+
eor ^= x
56+
lowbit = eor & (-eor)
57+
ans = [0, 0]
58+
for x in nums:
59+
if (x & lowbit) == 0:
60+
ans[0] ^= x
61+
ans[1] = eor ^ ans[0]
62+
return ans
6363
```
6464

6565
### **Java**
@@ -68,22 +68,84 @@ class Solution:
6868
class Solution {
6969
public int[] singleNumber(int[] nums) {
7070
int eor = 0;
71-
for (int num : nums) {
72-
eor ^= num;
71+
for (int x : nums) {
72+
eor ^= x;
7373
}
74-
int diff = eor & (~eor+1);
75-
int a = 0;
76-
for (int num : nums) {
77-
if ((num & diff) == 0) {
78-
a ^= num;
74+
int lowbit = eor & (-eor);
75+
int[] ans = newint[2];
76+
for (int x : nums) {
77+
if ((x & lowbit) == 0) {
78+
ans[0] ^= x;
7979
}
8080
}
81-
int b = eor ^ a;
82-
return newint[]{a, b};
81+
ans[1] = eor ^ ans[0];
82+
return ans;
8383
}
8484
}
8585
```
8686

87+
### **JavaScript**
88+
89+
```js
90+
/**
91+
* @param {number[]} nums
92+
* @return {number[]}
93+
*/
94+
var singleNumber = function(nums) {
95+
let eor = 0;
96+
for (const x of nums) {
97+
eor ^= x;
98+
}
99+
const lowbit = eor & (-eor);
100+
let ans = [0];
101+
for (const x of nums) {
102+
if ((x & lowbit) == 0) {
103+
ans[0] ^= x;
104+
}
105+
}
106+
ans.push(eor ^ ans[0]);
107+
return ans;
108+
};
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
vector<int> singleNumber(vector<int>& nums) {
117+
long long eor = 0;
118+
for (int x : nums) eor ^= x;
119+
int lowbit = eor & (-eor);
120+
vector<int> ans(2);
121+
for (int x : nums)
122+
if ((x & lowbit) == 0) ans[0] ^= x;
123+
ans[1] = eor ^ ans[0];
124+
return ans;
125+
}
126+
};
127+
```
128+
129+
### **Go**
130+
131+
```go
132+
func singleNumber(nums []int) []int {
133+
eor := 0
134+
for _, x := range nums {
135+
eor ^= x
136+
}
137+
lowbit := eor & (-eor)
138+
ans := make([]int, 2)
139+
for _, x := range nums {
140+
if (x & lowbit) == 0 {
141+
ans[0] ^= x
142+
}
143+
}
144+
ans[1] = eor ^ ans[0]
145+
return ans
146+
}
147+
```
148+
87149
### **...**
88150

89151
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
vector<int> singleNumber(vector<int>& nums) {
4+
long long eor = 0;
5+
for (int x : nums) eor ^= x;
6+
int lowbit = eor & (-eor);
7+
vector<int> ans(2);
8+
for (int x : nums)
9+
if ((x & lowbit) == 0) ans[0] ^= x;
10+
ans[1] = eor ^ ans[0];
11+
return ans;
12+
}
13+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func singleNumber(nums []int) []int {
2+
eor := 0
3+
for _, x := range nums {
4+
eor ^= x
5+
}
6+
lowbit := eor & (-eor)
7+
ans := make([]int, 2)
8+
for _, x := range nums {
9+
if (x & lowbit) == 0 {
10+
ans[0] ^= x
11+
}
12+
}
13+
ans[1] = eor ^ ans[0]
14+
return ans
15+
}
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
class Solution {
22
public int[] singleNumber(int[] nums) {
33
int eor = 0;
4-
for (int num : nums) {
5-
eor ^= num;
4+
for (int x : nums) {
5+
eor ^= x;
66
}
7-
// 提取最右的 1
8-
int diff = eor & (~eor + 1);
9-
int a = 0;
10-
for (int num : nums) {
11-
if ((num & diff) == 0) {
12-
a ^= num;
7+
int lowbit = eor & (-eor);
8+
int[] ans = new int[2];
9+
for (int x : nums) {
10+
if ((x & lowbit) == 0) {
11+
ans[0] ^= x;
1312
}
1413
}
15-
intb= eor ^ a;
16-
return newint[]{a, b};
14+
ans[1] = eor ^ ans[0];
15+
return ans;
1716
}
1817
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var singleNumber = function(nums) {
6+
let eor = 0;
7+
for (const x of nums) {
8+
eor ^= x;
9+
}
10+
const lowbit = eor & (-eor);
11+
let ans = [0];
12+
for (const x of nums) {
13+
if ((x & lowbit) == 0) {
14+
ans[0] ^= x;
15+
}
16+
}
17+
ans.push(eor ^ ans[0]);
18+
return ans;
19+
};
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
class Solution:
22
def singleNumber(self, nums: List[int]) -> List[int]:
33
eor = 0
4-
for num in nums:
5-
eor ^= num
6-
# 提取最右边的 1
7-
diff = eor & (~eor + 1)
8-
a = 0
9-
for num in nums:
10-
if (num & diff) == 0:
11-
a ^= num
12-
b = eor ^ a
13-
return [a, b]
4+
for x in nums:
5+
eor ^= x
6+
lowbit = eor & (-eor)
7+
ans = [0, 0]
8+
for x in nums:
9+
if (x & lowbit) == 0:
10+
ans[0] ^= x
11+
ans[1] = eor ^ ans[0]
12+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /