Skip to content

Commit ecbdb35

Browse files
committed
feat: add python and java solutions to lcof problem
添加《剑指 Offer》题解:面试题53 - II. 0~n-1中缺失的数字
1 parent 12e1d3c commit ecbdb35

File tree

5 files changed

+106
-2
lines changed

5 files changed

+106
-2
lines changed

README_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@
4646
<a href="https://opencollective.com/doocs-leetcode/contributors.svg?width=890&button=true"><img src="https://opencollective.com/doocs-leetcode/contributors.svg?width=890&button=false" /></a>
4747

4848
## 许可证
49-
<a rel="license" href="https://github.com/doocs/leetcode/blob/master/LICENSE"><img alt="Creative Commons License" style="border-width:0" src="./img/cc-by-sa-88x31.png" /></a><br /><a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">知识共享 版权归属-相同方式共享 4.0 国际 公共许可证</a>
49+
<a rel="license" href="https://github.com/doocs/leetcode/blob/master/LICENSE"><img alt="Creative Commons License" style="border-width:0" src="./img/cc-by-sa-88x31.png" /></a><br /><a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">知识共享 版权归属-相同方式共享 4.0 国际 公共许可证</a>

lcof/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
| [50](https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof) | [第一个只出现一次的字符](./%E9%9D%A2%E8%AF%95%E9%A2%9850.%20%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%8F%AA%E5%87%BA%E7%8E%B0%E4%B8%80%E6%AC%A1%E7%9A%84%E5%AD%97%E7%AC%A6) | 简单 |
5959
| [51](https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof) | [数组中的逆序对](./%E9%9D%A2%E8%AF%95%E9%A2%9851.%20%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E9%80%86%E5%BA%8F%E5%AF%B9) | 困难 |
6060
| [52](https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof) | [两个链表的第一个公共节点](./%E9%9D%A2%E8%AF%95%E9%A2%9852.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%85%AC%E5%85%B1%E8%8A%82%E7%82%B9) | 简单 |
61-
| [53 - II](https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof) | [缺失的数字](./%E9%9D%A2%E8%AF%95%E9%A2%9853%20-%20II.%20%E7%BC%BA%E5%A4%B1%E7%9A%84%E6%95%B0%E5%AD%97) | 简单 |
61+
| [53 - II](https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof) | [缺失的数字](./%E9%9D%A2%E8%AF%95%E9%A2%9853%20-%20II.%200%EF%BD%9En-1%E4%B8%AD%E7%BC%BA%E5%A4%B1%E7%9A%84%E6%95%B0%E5%AD%97) | 简单 |
6262
| [53 - I](https://leetcode-cn.com/problems/zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof) | [在排序数组中查找数字](./%E9%9D%A2%E8%AF%95%E9%A2%9853%20-%20I.%20%E5%9C%A8%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9F%A5%E6%89%BE%E6%95%B0%E5%AD%97%20I) | 简单 |
6363
| [54](https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof) | [二叉搜索树的第k大节点](./%E9%9D%A2%E8%AF%95%E9%A2%9854.%20%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E7%9A%84%E7%AC%ACk%E5%A4%A7%E8%8A%82%E7%82%B9) | 简单 |
6464
| [55 - II](https://leetcode-cn.com/problems/ping-heng-er-cha-shu-lcof) | [平衡二叉树](./%E9%9D%A2%E8%AF%95%E9%A2%9855%20-%20II.%20%E5%B9%B3%E8%A1%A1%E4%BA%8C%E5%8F%89%E6%A0%91) | 简单 |
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [面试题53 - II. 0~n-1中缺失的数字](https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof/)
2+
3+
## 题目描述
4+
一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字都在范围0~n-1之内。在范围0~n-1内的n个数字中有且只有一个数字不在该数组中,请找出这个数字。
5+
6+
**示例 1:**
7+
8+
```
9+
输入: [0,1,3]
10+
输出: 2
11+
```
12+
13+
**示例 2:**
14+
15+
```
16+
输入: [0,1,2,3,4,5,6,7,9]
17+
输出: 8
18+
```
19+
20+
**限制:**
21+
22+
- `1 <= 数组长度 <= 10000`
23+
24+
## 解法
25+
### Python3
26+
```python
27+
class Solution:
28+
def missingNumber(self, nums: List[int]) -> int:
29+
l, r = 0, len(nums) - 1
30+
if r == 0 or nums[0] == 1:
31+
return nums[0] ^ 1
32+
if nums[r] == r:
33+
return r + 1
34+
while r - l > 1:
35+
m = l + ((r - l) >> 1)
36+
if nums[m] == m:
37+
l = m
38+
else:
39+
r = m
40+
return nums[r] - 1
41+
```
42+
43+
### Java
44+
```java
45+
class Solution {
46+
public int missingNumber(int[] nums) {
47+
int l = 0, r = nums.length - 1;
48+
if (r == 0 || nums[0] == 1) {
49+
return nums[0] ^ 1;
50+
}
51+
if (nums[r] == r) {
52+
return r + 1;
53+
}
54+
while (r - l > 1) {
55+
int m = l + ((r - l) >> 1);
56+
if (nums[m] == m) {
57+
l = m;
58+
} else {
59+
r = m;
60+
}
61+
}
62+
return nums[r] - 1;
63+
}
64+
}
65+
```
66+
67+
### ...
68+
```
69+
70+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int missingNumber(int[] nums) {
3+
int l = 0, r = nums.length - 1;
4+
if (r == 0 || nums[0] == 1) {
5+
return nums[0] ^ 1;
6+
}
7+
if (nums[r] == r) {
8+
return r + 1;
9+
}
10+
while (r - l > 1) {
11+
int m = l + ((r - l) >> 1);
12+
if (nums[m] == m) {
13+
l = m;
14+
} else {
15+
r = m;
16+
}
17+
}
18+
return nums[r] - 1;
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def missingNumber(self, nums: List[int]) -> int:
3+
l, r = 0, len(nums) - 1
4+
if r == 0 or nums[0] == 1:
5+
return nums[0] ^ 1
6+
if nums[r] == r:
7+
return r + 1
8+
while r - l > 1:
9+
m = l + ((r - l) >> 1)
10+
if nums[m] == m:
11+
l = m
12+
else:
13+
r = m
14+
return nums[r] - 1

0 commit comments

Comments
 (0)