Skip to content

Commit ed0233e

Browse files
author
junfeng.fj
committed
addd new cpp solution for leetcode 322 [Coin Change]
2 parents 8f59352 + 257cd29 commit ed0233e

File tree

8 files changed

+94
-115
lines changed

8 files changed

+94
-115
lines changed

lcof/面试题56 - I. 数组中数字出现的次数/README.md

+28-39
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626

2727
异或运算求解。
2828

29-
首先明确,两个相同的数异或之后的结果为 0。对该数组所有元素进行异或运算,结果就是**两个只出现一次的数字异或的结果**。找出这个结果中某个二进制位为 1 的位置,之后对数组所有元素进行分类,二进制位为 0 的异或到 a,二进制位为 1 的异或到 b,结果就是 a,b。
29+
首先明确,两个相同的数异或之后的结果为 0。对该数组所有元素进行异或运算,结果就是**两个只出现一次的数字异或的结果**,即 `eor = a ^ b`
30+
31+
找出这个结果 eor 中最后一个二进制位为 1 而其余位为 0 的数,即 `eor & (~eor + 1)`,之后遍历数组所有元素,二进制位为 0 的元素异或到 a。
32+
33+
遍历结束后 `b = eor ^ a`,返回结果即可。
3034

3135
<!-- tabs:start -->
3236

@@ -35,21 +39,16 @@
3539
```python
3640
class Solution:
3741
def singleNumbers(self, nums: List[int]) -> List[int]:
38-
xor_res = 0
42+
eor = 0
3943
for num in nums:
40-
xor_res ^= num
41-
pos = 0
42-
while (xor_res & 1) == 0:
43-
pos += 1
44-
xor_res >>= 1
45-
46-
a = b = 0
44+
eor ^= num
45+
# 找出最右边的 1
46+
diff = eor & (~eor + 1)
47+
a = 0
4748
for num in nums:
48-
t = num >> pos
49-
if (t & 1) == 0:
49+
if (num & diff) == 0:
5050
a ^= num
51-
else:
52-
b ^= num
51+
b = eor ^ a
5352
return [a, b]
5453
```
5554

@@ -58,25 +57,20 @@ class Solution:
5857
```java
5958
class Solution {
6059
public int[] singleNumbers(int[] nums) {
61-
int xor = 0;
60+
int eor = 0;
6261
for (int num : nums) {
63-
xor ^= num;
62+
eor ^= num;
6463
}
65-
int pos = 0;
66-
while ((xor & 1) == 0) {
67-
++pos;
68-
xor >>= 1;
69-
}
70-
int a = 0, b = 0;
64+
// # 找出最右边的 1
65+
int diff = eor & (~eor + 1);
66+
int a = 0;
7167
for (int num : nums) {
72-
int t = num >> pos;
73-
if ((t & 1) == 0) {
68+
if ((num & diff) == 0) {
7469
a ^= num;
75-
} else {
76-
b ^= num;
7770
}
7871
}
79-
return new int[] {a, b};
72+
int b = eor ^ a;
73+
return new int[]{a, b};
8074
}
8175
}
8276
```
@@ -89,24 +83,19 @@ class Solution {
8983
* @return {number[]}
9084
*/
9185
var singleNumbers = function (nums) {
92-
let xor = 0;
93-
let bit = 1;
94-
let res = [0, 0];
86+
let eor = 0;
9587
for (let num of nums) {
96-
xor ^= num;
97-
}
98-
while ((xor & 1) === 0) {
99-
xor >>= 1;
100-
bit <<= 1;
88+
eor ^= num;
10189
}
90+
const diff = eor & (~eor + 1);
91+
let a = 0;
10292
for (let num of nums) {
103-
if ((num & bit) === 0) {
104-
res[0] ^= num;
105-
} else {
106-
res[1] ^= num;
93+
if ((num & diff) == 0) {
94+
a ^= num;
10795
}
10896
}
109-
return res;
97+
let b = eor ^ a;
98+
return [a, b];
11099
};
111100
```
112101

Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
class Solution {
22
public int[] singleNumbers(int[] nums) {
3-
int xor = 0;
3+
int eor = 0;
44
for (int num : nums) {
5-
xor ^= num;
5+
eor ^= num;
66
}
7-
int pos = 0;
8-
while ((xor & 1) == 0) {
9-
++pos;
10-
xor >>= 1;
11-
}
12-
int a = 0, b = 0;
7+
// # 找出最右边的 1
8+
int diff = eor & (~eor + 1);
9+
int a = 0;
1310
for (int num : nums) {
14-
int t = num >> pos;
15-
if ((t & 1) == 0) {
11+
if ((num & diff) == 0) {
1612
a ^= num;
17-
} else {
18-
b ^= num;
1913
}
2014
}
21-
return new int[] { a, b };
15+
int b = eor ^ a;
16+
return new int[]{a, b};
2217
}
2318
}

lcof/面试题56 - I. 数组中数字出现的次数/Solution.js

+8-13
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,17 @@
33
* @return {number[]}
44
*/
55
var singleNumbers = function (nums) {
6-
let xor = 0;
7-
let bit = 1;
8-
let res = [0, 0];
6+
let eor = 0;
97
for (let num of nums) {
10-
xor ^= num;
11-
}
12-
while ((xor & 1) === 0) {
13-
xor >>= 1;
14-
bit <<= 1;
8+
eor ^= num;
159
}
10+
const diff = eor & (~eor + 1);
11+
let a = 0;
1612
for (let num of nums) {
17-
if ((num & bit) === 0) {
18-
res[0] ^= num;
19-
} else {
20-
res[1] ^= num;
13+
if ((num & diff) == 0) {
14+
a ^= num;
2115
}
2216
}
23-
return res;
17+
let b = eor ^ a;
18+
return [a, b];
2419
};
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
class Solution:
22
def singleNumbers(self, nums: List[int]) -> List[int]:
3-
xor_res = 0
3+
eor = 0
44
for num in nums:
5-
xor_res ^= num
6-
pos = 0
7-
while (xor_res & 1) == 0:
8-
pos += 1
9-
xor_res >>= 1
10-
11-
a = b = 0
5+
eor ^= num
6+
# 找出最右边的 1
7+
diff = eor & (~eor + 1)
8+
a = 0
129
for num in nums:
13-
t = num >> pos
14-
if (t & 1) == 0:
10+
if (num & diff) == 0:
1511
a ^= num
16-
else:
17-
b ^= num
12+
b = eor ^ a
1813
return [a, b]

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

+15-13
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,16 @@
3232
```python
3333
class Solution:
3434
def singleNumber(self, nums: List[int]) -> List[int]:
35-
xor = 0
35+
eor = 0
3636
for num in nums:
37-
xor ^= num
38-
# x & (-x) 是保留位中最右边 1 ,且将其余的 1 设位 0 的方法
39-
diff = xor & (-xor)
40-
a = b = 0
37+
eor ^= num
38+
# 提取最右边的 1
39+
diff = eor & (~eor + 1)
40+
a = 0
4141
for num in nums:
4242
if (num & diff) == 0:
4343
a ^= num
44-
else:
45-
b ^= num
44+
b = eor ^ a
4645
return [a, b]
4746
```
4847

@@ -53,16 +52,19 @@ class Solution:
5352
```java
5453
class Solution {
5554
public int[] singleNumber(int[] nums) {
56-
int xor = 0;
55+
int eor = 0;
5756
for (int num : nums) {
58-
xor ^= num;
57+
eor ^= num;
5958
}
60-
int diff = xor & (-xor);
61-
int a = 0, b = 0;
59+
// 提取最右的 1
60+
int diff = eor & (~eor + 1);
61+
int a = 0;
6262
for (int num : nums) {
63-
if ((num & diff) == 0) a ^= num;
64-
else b ^= num;
63+
if ((num & diff) == 0) {
64+
a ^= num;
65+
}
6566
}
67+
int b = eor ^ a;
6668
return new int[]{a, b};
6769
}
6870
}

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

+13-13
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,35 @@
3030
```python
3131
class Solution:
3232
def singleNumber(self, nums: List[int]) -> List[int]:
33-
xor = 0
33+
eor = 0
3434
for num in nums:
35-
xor ^= num
36-
diff = xor & (-xor)
37-
a = b = 0
35+
eor ^= num
36+
diff = eor & (~eor + 1)
37+
a = 0
3838
for num in nums:
3939
if (num & diff) == 0:
4040
a ^= num
41-
else:
42-
b ^= num
41+
b = eor ^ a
4342
return [a, b]
44-
4543
```
4644

4745
### **Java**
4846

4947
```java
5048
class Solution {
5149
public int[] singleNumber(int[] nums) {
52-
int xor = 0;
50+
int eor = 0;
5351
for (int num : nums) {
54-
xor ^= num;
52+
eor ^= num;
5553
}
56-
int diff = xor & (-xor);
57-
int a = 0, b = 0;
54+
int diff = eor & (~eor + 1);
55+
int a = 0;
5856
for (int num : nums) {
59-
if ((num & diff) == 0) a ^= num;
60-
else b ^= num;
57+
if ((num & diff) == 0) {
58+
a ^= num;
59+
}
6160
}
61+
int b = eor ^ a;
6262
return new int[]{a, b};
6363
}
6464
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
class Solution {
22
public int[] singleNumber(int[] nums) {
3-
int xor = 0;
3+
int eor = 0;
44
for (int num : nums) {
5-
xor ^= num;
5+
eor ^= num;
66
}
7-
int diff = xor & (-xor);
8-
int a = 0, b = 0;
7+
// 提取最右的 1
8+
int diff = eor & (~eor + 1);
9+
int a = 0;
910
for (int num : nums) {
10-
if ((num & diff) == 0) a ^= num;
11-
else b ^= num;
11+
if ((num & diff) == 0) {
12+
a ^= num;
13+
}
1214
}
15+
int b = eor ^ a;
1316
return new int[]{a, b};
1417
}
1518
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution:
22
def singleNumber(self, nums: List[int]) -> List[int]:
3-
xor = 0
3+
eor = 0
44
for num in nums:
5-
xor ^= num
6-
diff = xor & (-xor)
7-
a = b = 0
5+
eor ^= num
6+
# 提取最右边的 1
7+
diff = eor & (~eor + 1)
8+
a = 0
89
for num in nums:
910
if (num & diff) == 0:
1011
a ^= num
11-
else:
12-
b ^= num
12+
b = eor ^ a
1313
return [a, b]

0 commit comments

Comments
 (0)