Skip to content

Commit 4595cb4

Browse files
committedAug 7, 2020
Merge remote-tracking branch 'origin/master'
2 parents 0713256 + e15cdf9 commit 4595cb4

File tree

13 files changed

+285
-32
lines changed

13 files changed

+285
-32
lines changed
 

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
- `1 <= nums[i] < 2^31`
2424

2525
## 解法
26+
27+
统计所有数字每个位中 1 出现的次数,对于某个位,1 出现的次数一定是 3 的倍数 +1 或 0。对这个数 %3 得到的结果就是那个出现一次的数字在该位上的值。
28+
29+
2630
<!-- tabs:start -->
2731

2832
### **Python3**
@@ -34,11 +38,10 @@ class Solution:
3438
for i in range(32):
3539
bits[i] += (num & 1)
3640
num >>= 1
37-
3841
res = 0
3942
for i in range(32):
4043
if bits[i] % 3 == 1:
41-
res += pow(2, i)
44+
res += (1 << i)
4245
return res
4346
```
4447

@@ -56,7 +59,7 @@ class Solution {
5659
int res = 0;
5760
for (int i = 0; i < 32; ++i) {
5861
if (bits[i] % 3 == 1) {
59-
res += (int) Math.pow(2, i);
62+
res += (1 << i);
6063
}
6164
}
6265
return res;

‎lcof/面试题56 - II. 数组中数字出现的次数 II/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public int singleNumber(int[] nums) {
1010
int res = 0;
1111
for (int i = 0; i < 32; ++i) {
1212
if (bits[i] % 3 == 1) {
13-
res += (int) Math.pow(2, i);
13+
res += (1 << i);
1414
}
1515
}
1616
return res;

‎lcof/面试题56 - II. 数组中数字出现的次数 II/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ def singleNumber(self, nums: List[int]) -> int:
55
for i in range(32):
66
bits[i] += (num & 1)
77
num >>= 1
8-
98
res = 0
109
for i in range(32):
1110
if bits[i] % 3 == 1:
12-
res += pow(2, i)
11+
res += (1 << i)
1312
return res

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,37 @@
2626
## 解法
2727
<!-- 这里可写通用的实现逻辑 -->
2828

29+
异或运算求解。
30+
31+
首先明确,两个相同的数异或之后的结果为 0。对该数组所有元素进行异或运算,结果就是那个只出现一次的数字。
2932

3033
<!-- tabs:start -->
3134

3235
### **Python3**
3336
<!-- 这里可写当前语言的特殊实现逻辑 -->
3437

3538
```python
36-
39+
class Solution:
40+
def singleNumber(self, nums: List[int]) -> int:
41+
res = 0
42+
for num in nums:
43+
res ^= num
44+
return res
3745
```
3846

3947
### **Java**
4048
<!-- 这里可写当前语言的特殊实现逻辑 -->
4149

4250
```java
43-
51+
class Solution {
52+
public int singleNumber(int[] nums) {
53+
int res = 0;
54+
for (int num : nums) {
55+
res ^= num;
56+
}
57+
return res;
58+
}
59+
}
4460
```
4561

4662
### **...**

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,26 @@
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def singleNumber(self, nums: List[int]) -> int:
57+
res = 0
58+
for num in nums:
59+
res ^= num
60+
return res
5661
```
5762

5863
### **Java**
5964

6065
```java
61-
66+
class Solution {
67+
public int singleNumber(int[] nums) {
68+
int res = 0;
69+
for (int num : nums) {
70+
res ^= num;
71+
}
72+
return res;
73+
}
74+
}
6275
```
6376

6477
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
class Solution:
2-
def singleNumber(self, nums):
3-
"""
4-
:type nums: List[int]
5-
:rtype: int
6-
"""
7-
res=0
8-
for i in nums:
9-
res = res^i
10-
return res
2+
def singleNumber(self, nums: List[int]) -> int:
3+
res = 0
4+
for num in nums:
5+
res ^= num
6+
return res

‎solution/0100-0199/0137.Single Number II/README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
## 解法
2727
<!-- 这里可写通用的实现逻辑 -->
2828

29+
统计所有数字每个位中 1 出现的次数,对于某个位,1 出现的次数一定是 3 的倍数 +1 或 0。对这个数 %3 得到的结果就是那个出现一次的数字在该位上的值。
2930

3031
<!-- tabs:start -->
3132

@@ -40,7 +41,25 @@
4041
<!-- 这里可写当前语言的特殊实现逻辑 -->
4142

4243
```java
43-
44+
class Solution {
45+
public int singleNumber(int[] nums) {
46+
int[] bits = new int[32];
47+
for (int num : nums) {
48+
for (int i = 0; i < 32; ++i) {
49+
bits[i] += (num & 1);
50+
num >>= 1;
51+
}
52+
}
53+
54+
int res = 0;
55+
for (int i = 0; i < 32; ++i) {
56+
if (bits[i] % 3 == 1) {
57+
res += (1 << i);
58+
}
59+
}
60+
return res;
61+
}
62+
}
4463
```
4564

4665
### **...**

‎solution/0100-0199/0137.Single Number II/README_EN.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,25 @@
5656
### **Java**
5757

5858
```java
59-
59+
class Solution {
60+
public int singleNumber(int[] nums) {
61+
int[] bits = new int[32];
62+
for (int num : nums) {
63+
for (int i = 0; i < 32; ++i) {
64+
bits[i] += (num & 1);
65+
num >>= 1;
66+
}
67+
}
68+
69+
int res = 0;
70+
for (int i = 0; i < 32; ++i) {
71+
if (bits[i] % 3 == 1) {
72+
res += (1 << i);
73+
}
74+
}
75+
return res;
76+
}
77+
}
6078
```
6179

6280
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
class Solution {
22
public int singleNumber(int[] nums) {
33
int[] bits = new int[32];
4-
int n = nums.length;
5-
for (int i = 0; i < n; ++i) {
6-
for (int j = 0; j < 32; ++j) {
7-
bits[j] += ((nums[i] >> j) & 1);
4+
for (int num : nums) {
5+
for (int i = 0; i < 32; ++i) {
6+
bits[i] += (num & 1);
7+
num >>= 1;
88
}
99
}
10-
10+
1111
int res = 0;
1212
for (int i = 0; i < 32; ++i) {
13-
if (bits[i] % 3 != 0) {
13+
if (bits[i] % 3 == 1) {
1414
res += (1 << i);
1515
}
1616
}
1717
return res;
18-
1918
}
2019
}

‎solution/0600-0699/0645.Set Mismatch/README.md

+68-2
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,87 @@
2727
## 解法
2828
<!-- 这里可写通用的实现逻辑 -->
2929

30+
首先使用 1 到 n 的所有数字做异或运算,然后再与数组中的所有数字异或,得到的值就是缺失数字与重复的数字异或的结果。
31+
32+
接着计算中这个值中其中一个非零的位 pos。然后 pos 位是否为 1,将 nums 数组的元素分成两部分,分别异或;接着将 `1~n` 的元素也分成两部分,分别异或。得到的两部分结果分别为 a,b,即是缺失数字与重复数字。
33+
34+
最后判断数组中是否存在 a 或 b,若存在 a,说明重复数字是 a,返回 `[a,b]`,否则返回 `[b,a]`
3035

3136
<!-- tabs:start -->
3237

3338
### **Python3**
3439
<!-- 这里可写当前语言的特殊实现逻辑 -->
3540

3641
```python
37-
42+
class Solution:
43+
def findErrorNums(self, nums: List[int]) -> List[int]:
44+
res = 0
45+
for num in nums:
46+
res ^= num
47+
for i in range(1, len(nums) + 1):
48+
res ^= i
49+
pos = 0
50+
while (res & 1) == 0:
51+
res >>= 1
52+
pos += 1
53+
a = b = 0
54+
for num in nums:
55+
if ((num >> pos) & 1) == 0:
56+
a ^= num
57+
else:
58+
b ^= num
59+
for i in range(1, len(nums) + 1):
60+
if ((i >> pos) & 1) == 0:
61+
a ^= i
62+
else:
63+
b ^= i
64+
for num in nums:
65+
if num == a:
66+
return [a, b]
67+
return [b, a]
3868
```
3969

4070
### **Java**
4171
<!-- 这里可写当前语言的特殊实现逻辑 -->
4272

4373
```java
44-
74+
class Solution {
75+
public int[] findErrorNums(int[] nums) {
76+
int res = 0;
77+
for (int num : nums) {
78+
res ^= num;
79+
}
80+
for (int i = 1, n = nums.length; i < n + 1; ++i) {
81+
res ^= i;
82+
}
83+
int pos = 0;
84+
while ((res & 1) == 0) {
85+
res >>= 1;
86+
++pos;
87+
}
88+
int a = 0, b = 0;
89+
for (int num : nums) {
90+
if (((num >> pos) & 1) == 0) {
91+
a ^= num;
92+
} else {
93+
b ^= num;
94+
}
95+
}
96+
for (int i = 1, n = nums.length; i < n + 1; ++i) {
97+
if (((i >> pos) & 1) == 0) {
98+
a ^= i;
99+
} else {
100+
b ^= i;
101+
}
102+
}
103+
for (int num : nums) {
104+
if (num == a) {
105+
return new int[]{a, b};
106+
}
107+
}
108+
return new int[]{b, a};
109+
}
110+
}
45111
```
46112

47113
### **...**

‎solution/0600-0699/0645.Set Mismatch/README_EN.md

+63-2
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,74 @@ Given an array <code>nums</code> representing the data status of this set after
5656
### **Python3**
5757

5858
```python
59-
59+
class Solution:
60+
def findErrorNums(self, nums: List[int]) -> List[int]:
61+
res = 0
62+
for num in nums:
63+
res ^= num
64+
for i in range(1, len(nums) + 1):
65+
res ^= i
66+
pos = 0
67+
while (res & 1) == 0:
68+
res >>= 1
69+
pos += 1
70+
a = b = 0
71+
for num in nums:
72+
if ((num >> pos) & 1) == 0:
73+
a ^= num
74+
else:
75+
b ^= num
76+
for i in range(1, len(nums) + 1):
77+
if ((i >> pos) & 1) == 0:
78+
a ^= i
79+
else:
80+
b ^= i
81+
for num in nums:
82+
if num == a:
83+
return [a, b]
84+
return [b, a]
6085
```
6186

6287
### **Java**
6388

6489
```java
65-
90+
class Solution {
91+
public int[] findErrorNums(int[] nums) {
92+
int res = 0;
93+
for (int num : nums) {
94+
res ^= num;
95+
}
96+
for (int i = 1, n = nums.length; i < n + 1; ++i) {
97+
res ^= i;
98+
}
99+
int pos = 0;
100+
while ((res & 1) == 0) {
101+
res >>= 1;
102+
++pos;
103+
}
104+
int a = 0, b = 0;
105+
for (int num : nums) {
106+
if (((num >> pos) & 1) == 0) {
107+
a ^= num;
108+
} else {
109+
b ^= num;
110+
}
111+
}
112+
for (int i = 1, n = nums.length; i < n + 1; ++i) {
113+
if (((i >> pos) & 1) == 0) {
114+
a ^= i;
115+
} else {
116+
b ^= i;
117+
}
118+
}
119+
for (int num : nums) {
120+
if (num == a) {
121+
return new int[]{a, b};
122+
}
123+
}
124+
return new int[]{b, a};
125+
}
126+
}
66127
```
67128

68129
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public int[] findErrorNums(int[] nums) {
3+
int res = 0;
4+
for (int num : nums) {
5+
res ^= num;
6+
}
7+
for (int i = 1, n = nums.length; i < n + 1; ++i) {
8+
res ^= i;
9+
}
10+
int pos = 0;
11+
while ((res & 1) == 0) {
12+
res >>= 1;
13+
++pos;
14+
}
15+
int a = 0, b = 0;
16+
for (int num : nums) {
17+
if (((num >> pos) & 1) == 0) {
18+
a ^= num;
19+
} else {
20+
b ^= num;
21+
}
22+
}
23+
for (int i = 1, n = nums.length; i < n + 1; ++i) {
24+
if (((i >> pos) & 1) == 0) {
25+
a ^= i;
26+
} else {
27+
b ^= i;
28+
}
29+
}
30+
for (int num : nums) {
31+
if (num == a) {
32+
return new int[]{a, b};
33+
}
34+
}
35+
return new int[]{b, a};
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def findErrorNums(self, nums: List[int]) -> List[int]:
3+
res = 0
4+
for num in nums:
5+
res ^= num
6+
for i in range(1, len(nums) + 1):
7+
res ^= i
8+
pos = 0
9+
while (res & 1) == 0:
10+
res >>= 1
11+
pos += 1
12+
a = b = 0
13+
for num in nums:
14+
if ((num >> pos) & 1) == 0:
15+
a ^= num
16+
else:
17+
b ^= num
18+
for i in range(1, len(nums) + 1):
19+
if ((i >> pos) & 1) == 0:
20+
a ^= i
21+
else:
22+
b ^= i
23+
for num in nums:
24+
if num == a:
25+
return [a, b]
26+
return [b, a]

0 commit comments

Comments
 (0)
Please sign in to comment.