Skip to content

Commit cff0890

Browse files
authored
feat: add binary search solutions (#472)
1 parent f603b42 commit cff0890

File tree

19 files changed

+628
-484
lines changed

19 files changed

+628
-484
lines changed

README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
4343

4444
### Searching
4545

46-
- [Binary Search(Algorithm Template)](./basic/searching/BinarySearch/README.md)
46+
- [Binary Search(Algorithm Template)](./basic/searching/BinarySearch/README_EN.md)
4747

4848
## High Frequency Interview Questions
4949

basic/searching/BinarySearch/Main.java

-40
This file was deleted.

basic/searching/BinarySearch/Main.py

-24
This file was deleted.

basic/searching/BinarySearch/README.md

+2-124
Original file line numberDiff line numberDiff line change
@@ -29,128 +29,6 @@ int binarySearch2(int left, int right) {
2929
}
3030
```
3131

32-
## 题目描述
32+
## 例题
3333

34-
给定一个按照升序排列的长度为 `n` 的整数数组,以及 `q` 个查询。
35-
36-
对于每个查询,返回一个元素 k 的起始位置和终止位置(位置从 0 开始计数)。
37-
38-
如果数组中不存在该元素,则返回 `-1 -1`
39-
40-
**输入格式**
41-
42-
第一行包含整数 n 和 q,表示数组长度和询问个数。
43-
44-
第二行包含 n 个整数(均在 1∼10000 范围内),表示完整数组。
45-
46-
接下来 q 行,每行包含一个整数 k,表示一个询问元素。
47-
48-
**输出格式**
49-
50-
共 q 行,每行包含两个整数,表示所求元素的起始位置和终止位置。
51-
52-
如果数组中不存在该元素,则返回 `-1 -1`
53-
54-
**数据范围**
55-
56-
- 1≤n≤100000
57-
- 1≤q≤10000
58-
- 1≤k≤10000
59-
60-
**输入样例:**
61-
62-
```
63-
6 3
64-
1 2 2 3 3 4
65-
3
66-
4
67-
5
68-
```
69-
70-
**输出样例:**
71-
72-
```
73-
3 4
74-
5 5
75-
-1 -1
76-
```
77-
78-
## 代码实现
79-
80-
<!-- tabs:start -->
81-
82-
### **Python3**
83-
84-
```python
85-
n, q = map(int, input().split())
86-
nums = list(map(int, input().split()))
87-
88-
for _ in range(q):
89-
x = int(input())
90-
left, right = 0, n - 1
91-
while left < right:
92-
mid = (left + right) >> 1
93-
if nums[mid] >= x:
94-
right = mid
95-
else:
96-
left = mid + 1
97-
if nums[left] != x:
98-
print('-1 -1')
99-
else:
100-
t = left
101-
left, right = 0, n - 1
102-
while left < right:
103-
mid = (left + right + 1) >> 1
104-
if nums[mid] <= x:
105-
left = mid
106-
else:
107-
right = mid - 1
108-
print(f'{t} {left}')
109-
```
110-
111-
### **Java**
112-
113-
```java
114-
import java.util.Scanner;
115-
116-
public class Main {
117-
public static void main(String[] args) {
118-
Scanner sc = new Scanner(System.in);
119-
int n = sc.nextInt(), q = sc.nextInt();
120-
int[] nums = new int[n];
121-
for (int i = 0; i < n; ++i) {
122-
nums[i] = sc.nextInt();
123-
}
124-
while (q-- > 0) {
125-
int x = sc.nextInt();
126-
int left = 0, right = n - 1;
127-
while (left < right) {
128-
int mid = (left + right) >> 1;
129-
if (nums[mid] >= x) {
130-
right = mid;
131-
} else {
132-
left = mid + 1;
133-
}
134-
}
135-
if (nums[left] != x) {
136-
System.out.println("-1 -1");
137-
} else {
138-
int t = left;
139-
left = 0;
140-
right = n - 1;
141-
while (left < right) {
142-
int mid = (left + right + 1) >> 1;
143-
if (nums[mid] <= x) {
144-
left = mid;
145-
} else {
146-
right = mid - 1;
147-
}
148-
}
149-
System.out.printf("%d %d\n", t, left);
150-
}
151-
}
152-
}
153-
}
154-
```
155-
156-
<!-- tabs:end -->
34+
- [在排序数组中查找元素的第一个和最后一个位置](/solution/0000-0099/0034.Find%20First%20and%20Last%20Position%20of%20Element%20in%20Sorted%20Array/README.md)
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Binary Search
2+
3+
**Algorithm Templates:**
4+
5+
```java
6+
/** check x if valid */
7+
boolean check(int x) {}
8+
9+
/** template1 */
10+
int binarySearch1(int left, int right) {
11+
while (left < right) {
12+
int mid = (left + right) >> 1;
13+
if (check(mid)) right = mid;
14+
else left = mid + 1;
15+
}
16+
return left;
17+
}
18+
19+
/** template2 */
20+
int binarySearch2(int left, int right) {
21+
while (left < right) {
22+
int mid = (left + right + 1) >> 1;
23+
if (check(mid)) left = mid;
24+
else right = mid - 1;
25+
}
26+
return left;
27+
}
28+
```
29+
30+
## Examples
31+
32+
- [Find First and Last Position of Element in Sorted Array](/solution/0000-0099/0034.Find%20First%20and%20Last%20Position%20of%20Element%20in%20Sorted%20Array/README_EN.md)

0 commit comments

Comments
 (0)