Skip to content

Commit 2cc8917

Browse files
committed
feat: add python and java solutions to lcci question: 17.19.Missing Two
See https://lc.netlify.app/#/lcci/17.19.Missing%20Two/README
1 parent 9e1cc06 commit 2cc8917

File tree

7 files changed

+202
-11
lines changed

7 files changed

+202
-11
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
<p align="center">
66
<a href="https://github.com/doocs/leetcode"><img src="https://badgen.net/badge/langs/Java,C++,Python,JavaScript,Go,.../green?list=1" alt="languages"></a>
77
<a href="https://github.com/doocs/leetcode/stargazers"><img src="https://badgen.net/github/stars/doocs/leetcode" alt="stars"></a>
8-
<a href="https://github.com/doocs/leetcode/issues"><img src="https://badgen.net/github/open-issues/doocs/leetcode" alt="issues"></a>
9-
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://img.shields.io/github/forks/doocs/leetcode.svg" alt="forks"></a>
8+
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://badgen.net/github/forks/doocs/leetcode" alt="forks"></a>
109
<a href="https://github.com/doocs/leetcode/blob/master/LICENSE"><img src="https://badgen.net/github/license/doocs/leetcode?color=green" alt="LICENSE"></a><br>
1110
<a href="http://makeapullrequest.com"><img src="https://badgen.net/badge/PRs/welcome/cyan" alt="PRs Welcome"></a>
1211
<a href="https://doocs.github.io/#/?id=how-to-join"><img src="https://badgen.net/badge/organization/join%20us/cyan" alt="open-source-organization"></a>
@@ -18,7 +17,7 @@
1817
[English Version](/README_EN.md)
1918

2019
## 站点
21-
- Netlify: https://lc.netlify.com
20+
- Netlify: https://lc.netlify.app
2221
- GitHub Pages: https://doocs.github.io/leetcode
2322
- Coding Pages: https://d3jc40.coding-pages.com
2423

README_EN.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
<p align="center">
66
<a href="https://github.com/doocs/leetcode"><img src="https://badgen.net/badge/langs/Java,C++,Python,JavaScript,Go,.../green?list=1" alt="languages"></a>
77
<a href="https://github.com/doocs/leetcode/stargazers"><img src="https://badgen.net/github/stars/doocs/leetcode" alt="stars"></a>
8-
<a href="https://github.com/doocs/leetcode/issues"><img src="https://badgen.net/github/open-issues/doocs/leetcode" alt="issues"></a>
9-
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://img.shields.io/github/forks/doocs/leetcode.svg" alt="forks"></a>
8+
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://badgen.net/github/forks/doocs/leetcode" alt="forks"></a>
109
<a href="https://github.com/doocs/leetcode/blob/master/LICENSE"><img src="https://badgen.net/github/license/doocs/leetcode?color=green" alt="LICENSE"></a><br>
1110
<a href="http://makeapullrequest.com"><img src="https://badgen.net/badge/PRs/welcome/cyan" alt="PRs Welcome"></a>
1211
<a href="https://doocs.github.io/#/?id=how-to-join"><img src="https://badgen.net/badge/organization/join%20us/cyan" alt="open-source-organization"></a>
@@ -18,7 +17,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
1817
[中文文档](/README.md)
1918

2019
## Sites
21-
- Netlify: https://lc.netlify.com
20+
- Netlify: https://lc.netlify.app
2221
- GitHub Pages: https://doocs.github.io/leetcode
2322
- Coding Pages: https://d3jc40.coding-pages.com
2423

lcci/17.19.Missing Two/README.md

+65-3
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,82 @@
2525

2626
## 解法
2727
<!-- 这里可写通用的实现逻辑 -->
28-
28+
异或运算。与[面试题56 - I. 数组中数字出现的次数](/lcof/面试题56%20-%20I.%20数组中数字出现的次数/README.md) 类似。
2929

3030
### Python3
3131
<!-- 这里可写当前语言的特殊实现逻辑 -->
3232

3333
```python
34-
34+
class Solution:
35+
def missingTwo(self, nums: List[int]) -> List[int]:
36+
res, n = 0, len(nums)
37+
for i in range(n):
38+
res ^= nums[i]
39+
res ^= (i + 1)
40+
res ^= (n + 1)
41+
res ^= (n + 2)
42+
pos = 0
43+
while (res & 1) == 0:
44+
pos += 1
45+
res >>= 1
46+
47+
a = b = 0
48+
for num in nums:
49+
t = num >> pos
50+
if (t & 1) == 0:
51+
a ^= num
52+
else:
53+
b ^= num
54+
55+
for i in range(1, n + 3):
56+
t = i >> pos
57+
if (t & 1) == 0:
58+
a ^= i
59+
else:
60+
b ^= i
61+
return [a, b]
3562
```
3663

3764
### Java
3865
<!-- 这里可写当前语言的特殊实现逻辑 -->
3966

4067
```java
41-
68+
class Solution {
69+
public int[] missingTwo(int[] nums) {
70+
int res = 0, n = nums.length;
71+
for (int i = 0; i < n; ++i) {
72+
res ^= nums[i];
73+
res ^= (i + 1);
74+
}
75+
res ^= (n + 1);
76+
res ^= (n + 2);
77+
78+
int pos = 0;
79+
while ((res & 1) == 0) {
80+
pos += 1;
81+
res >>= 1;
82+
}
83+
84+
int a = 0, b = 0;
85+
for (int num : nums) {
86+
int t = num >> pos;
87+
if ((t & 1) == 0) {
88+
a ^= num;
89+
} else {
90+
b ^= num;
91+
}
92+
}
93+
for (int i = 1; i <= n + 2; ++i) {
94+
int t = i >> pos;
95+
if ((t & 1) == 0) {
96+
a ^= i;
97+
} else {
98+
b ^= i;
99+
}
100+
}
101+
return new int[]{a, b};
102+
}
103+
}
42104
```
43105

44106
### ...

lcci/17.19.Missing Two/README_EN.md

+64-2
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,75 @@
5050
### Python3
5151

5252
```python
53-
53+
class Solution:
54+
def missingTwo(self, nums: List[int]) -> List[int]:
55+
res, n = 0, len(nums)
56+
for i in range(n):
57+
res ^= nums[i]
58+
res ^= (i + 1)
59+
res ^= (n + 1)
60+
res ^= (n + 2)
61+
pos = 0
62+
while (res & 1) == 0:
63+
pos += 1
64+
res >>= 1
65+
66+
a = b = 0
67+
for num in nums:
68+
t = num >> pos
69+
if (t & 1) == 0:
70+
a ^= num
71+
else:
72+
b ^= num
73+
74+
for i in range(1, n + 3):
75+
t = i >> pos
76+
if (t & 1) == 0:
77+
a ^= i
78+
else:
79+
b ^= i
80+
return [a, b]
5481
```
5582

5683
### Java
5784

5885
```java
59-
86+
class Solution {
87+
public int[] missingTwo(int[] nums) {
88+
int res = 0, n = nums.length;
89+
for (int i = 0; i < n; ++i) {
90+
res ^= nums[i];
91+
res ^= (i + 1);
92+
}
93+
res ^= (n + 1);
94+
res ^= (n + 2);
95+
96+
int pos = 0;
97+
while ((res & 1) == 0) {
98+
pos += 1;
99+
res >>= 1;
100+
}
101+
102+
int a = 0, b = 0;
103+
for (int num : nums) {
104+
int t = num >> pos;
105+
if ((t & 1) == 0) {
106+
a ^= num;
107+
} else {
108+
b ^= num;
109+
}
110+
}
111+
for (int i = 1; i <= n + 2; ++i) {
112+
int t = i >> pos;
113+
if ((t & 1) == 0) {
114+
a ^= i;
115+
} else {
116+
b ^= i;
117+
}
118+
}
119+
return new int[]{a, b};
120+
}
121+
}
60122
```
61123

62124
### ...

lcci/17.19.Missing Two/Solution.java

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

lcci/17.19.Missing Two/Solution.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def missingTwo(self, nums: List[int]) -> List[int]:
3+
res, n = 0, len(nums)
4+
for i in range(n):
5+
res ^= nums[i]
6+
res ^= (i + 1)
7+
res ^= (n + 1)
8+
res ^= (n + 2)
9+
pos = 0
10+
while (res & 1) == 0:
11+
pos += 1
12+
res >>= 1
13+
14+
a = b = 0
15+
for num in nums:
16+
t = num >> pos
17+
if (t & 1) == 0:
18+
a ^= num
19+
else:
20+
b ^= num
21+
22+
for i in range(1, n + 3):
23+
t = i >> pos
24+
if (t & 1) == 0:
25+
a ^= i
26+
else:
27+
b ^= i
28+
return [a, b]

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

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
- `2 <= nums <= 10000`
2323

2424
## 解法
25+
异或运算求解。
26+
27+
首先明确,两个相同的数异或之后的结果为 0。对该数组所有元素进行异或运算,结果就是**两个只出现一次的数字异或的结果**。找出这个结果中某个二进制位为 1 的位置,之后对数组所有元素进行分类,二进制位为 0 的异或到 a,二进制位为 1 的异或到 b,结果就是 a,b。
28+
29+
2530
### Python3
2631
```python
2732
class Solution:

0 commit comments

Comments
 (0)