Skip to content

Commit 12e1d3c

Browse files
committed
feat: add python and java solutions to lcof problem
添加《剑指 Offer》题解:面试题53 - I. 在排序数组中查找数字 I
1 parent 69fb7b0 commit 12e1d3c

File tree

4 files changed

+173
-1
lines changed

4 files changed

+173
-1
lines changed

lcof/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
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) | 简单 |
6161
| [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) | 简单 |
62-
| [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) | 简单 |
62+
| [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) | 简单 |
6565
| [55 - I](https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof) | [二叉树的深度](./%E9%9D%A2%E8%AF%95%E9%A2%9855%20-%20I.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%B7%B1%E5%BA%A6) | 简单 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# [面试题53 - I. 在排序数组中查找数字 I](https://leetcode-cn.com/problems/zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof/)
2+
3+
## 题目描述
4+
统计一个数字在排序数组中出现的次数。
5+
6+
**示例 1:**
7+
8+
```
9+
输入: nums = [5,7,7,8,8,10], target = 8
10+
输出: 2
11+
```
12+
13+
**示例 2:**
14+
15+
```
16+
输入: nums = [5,7,7,8,8,10], target = 6
17+
输出: 0
18+
```
19+
20+
**限制:**
21+
22+
- `0 <= 数组长度 <= 50000`
23+
24+
## 解法
25+
### Python3
26+
```python
27+
class Solution:
28+
def search(self, nums: List[int], target: int) -> int:
29+
if not nums:
30+
return 0
31+
l, r = 0, len(nums) - 1
32+
while l <= r:
33+
m = l + ((r - l) >> 1)
34+
if nums[m] == target:
35+
return self._count(nums, l, r, m)
36+
if nums[m] < target:
37+
l = m + 1
38+
else:
39+
r = m - 1
40+
return 0
41+
42+
def _count(self, nums, l, r, m) -> int:
43+
cnt = 0
44+
for i in range(m, l - 1, -1):
45+
if nums[i] == nums[m]:
46+
cnt += 1
47+
elif nums[i] < nums[m]:
48+
break
49+
50+
for i in range(m + 1, r + 1):
51+
if nums[i] == nums[m]:
52+
cnt += 1
53+
elif nums[i] > nums[m]:
54+
break
55+
return cnt
56+
```
57+
58+
### Java
59+
```java
60+
class Solution {
61+
public int search(int[] nums, int target) {
62+
if (nums.length == 0) {
63+
return 0;
64+
}
65+
int l = 0, r = nums.length - 1;
66+
while (l <= r) {
67+
int m = l + ((r - l) >> 1);
68+
if (nums[m] == target) {
69+
return count(nums, l, r, m);
70+
}
71+
if (nums[m] < target) {
72+
l = m + 1;
73+
} else {
74+
r = m - 1;
75+
}
76+
}
77+
return 0;
78+
}
79+
80+
private int count(int[] nums, int l, int r, int m) {
81+
int cnt = 0;
82+
for (int i = m; i >= l; --i) {
83+
if (nums[i] == nums[m]) {
84+
++cnt;
85+
} else if (nums[i] < nums[m]) {
86+
break;
87+
}
88+
}
89+
for (int i = m + 1; i <= r; ++i) {
90+
if (nums[i] == nums[m]) {
91+
++cnt;
92+
} else if (nums[i] > nums[m]) {
93+
break;
94+
}
95+
}
96+
return cnt;
97+
}
98+
}
99+
```
100+
101+
### ...
102+
```
103+
104+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public int search(int[] nums, int target) {
3+
if (nums.length == 0) {
4+
return 0;
5+
}
6+
int l = 0, r = nums.length - 1;
7+
while (l <= r) {
8+
int m = l + ((r - l) >> 1);
9+
if (nums[m] == target) {
10+
return count(nums, l, r, m);
11+
}
12+
if (nums[m] < target) {
13+
l = m + 1;
14+
} else {
15+
r = m - 1;
16+
}
17+
}
18+
return 0;
19+
}
20+
21+
private int count(int[] nums, int l, int r, int m) {
22+
int cnt = 0;
23+
for (int i = m; i >= l; --i) {
24+
if (nums[i] == nums[m]) {
25+
++cnt;
26+
} else if (nums[i] < nums[m]) {
27+
break;
28+
}
29+
}
30+
for (int i = m + 1; i <= r; ++i) {
31+
if (nums[i] == nums[m]) {
32+
++cnt;
33+
} else if (nums[i] > nums[m]) {
34+
break;
35+
}
36+
}
37+
return cnt;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def search(self, nums: List[int], target: int) -> int:
3+
if not nums:
4+
return 0
5+
l, r = 0, len(nums) - 1
6+
while l <= r:
7+
m = l + ((r - l) >> 1)
8+
if nums[m] == target:
9+
return self._count(nums, l, r, m)
10+
if nums[m] < target:
11+
l = m + 1
12+
else:
13+
r = m - 1
14+
return 0
15+
16+
def _count(self, nums, l, r, m) -> int:
17+
cnt = 0
18+
for i in range(m, l - 1, -1):
19+
if nums[i] == nums[m]:
20+
cnt += 1
21+
elif nums[i] < nums[m]:
22+
break
23+
24+
for i in range(m + 1, r + 1):
25+
if nums[i] == nums[m]:
26+
cnt += 1
27+
elif nums[i] > nums[m]:
28+
break
29+
return cnt

0 commit comments

Comments
 (0)