You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README_EN.md
-1
Original file line number
Diff line number
Diff line change
@@ -43,7 +43,6 @@ The repository is maintained by the Doocs community, and please give us a [star]
43
43
44
44
-[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) - `Binary search`
45
45
-[Minimum Speed to Arrive on Time](/solution/1800-1899/1870.Minimum%20Speed%20to%20Arrive%20on%20Time/README_EN.md) - `Binary search`
46
-
-[Find the Student that Will Replace the Chalk](/solution/1800-1899/1894.Find%20the%20Student%20that%20Will%20Replace%20the%20Chalk/README_EN.md) - `Binary search`
47
46
-[Maximum Number of Removable Characters](/solution/1800-1899/1898.Maximum%20Number%20of%20Removable%20Characters/README_EN.md) - `Binary search`
48
47
-[Sort an Array](/solution/0900-0999/0912.Sort%20an%20Array/README_EN.md) - `Quick Sort`, `Merge Sort`
49
48
-[Add Strings](/solution/0400-0499/0415.Add%20Strings/README_EN.md) - `Addition of large numbers`
Copy file name to clipboardexpand all lines: solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README_EN.md
+39-29
Original file line number
Diff line number
Diff line change
@@ -79,6 +79,17 @@ int search(int left, int right) {
79
79
}
80
80
```
81
81
82
+
When doing binary search problems, you can follow the following routine:
83
+
84
+
1. Write out the loop condition $left < right$;
85
+
2. Inside the loop, you might as well write $mid = \lfloor \frac{left + right}{2} \rfloor$ first;
86
+
3. According to the specific problem, implement the $check()$ function (sometimes the logic is very simple, you can not define $check$), think about whether to use $right = mid$ (Template $1$) or $left = mid$ (Template $2$);
87
+
- If $right = mid$, then write the else statement $left = mid + 1$, and there is no need to change the calculation of $mid$, that is, keep $mid = \lfloor \frac{left + right}{2} \rfloor$;
88
+
- If $left = mid$, then write the else statement $right = mid - 1$, and add +1 when calculating $mid$, that is, $mid = \lfloor \frac{left + right + 1}{2} \rfloor$;
89
+
4. When the loop ends, $left$ equals $right$.
90
+
91
+
Note that the advantage of these two templates is that they always keep the answer within the binary search interval, and the value corresponding to the end condition of the binary search is exactly at the position of the answer. For the case that may have no solution, just check whether the $left$ or $right$ after the binary search ends satisfies the problem.
92
+
82
93
<!-- tabs:start -->
83
94
84
95
### **Python3**
@@ -130,34 +141,6 @@ public:
130
141
};
131
142
```
132
143
133
-
### **JavaScript**
134
-
135
-
```js
136
-
/**
137
-
* @param {number[]} nums
138
-
* @param {number} target
139
-
* @return {number[]}
140
-
*/
141
-
var searchRange = function (nums, target) {
142
-
function search(x) {
143
-
let left = 0,
144
-
right = nums.length;
145
-
while (left < right) {
146
-
const mid = (left + right) >> 1;
147
-
if (nums[mid] >= x) {
148
-
right = mid;
149
-
} else {
150
-
left = mid + 1;
151
-
}
152
-
}
153
-
return left;
154
-
}
155
-
const l = search(target);
156
-
const r = search(target + 1);
157
-
return l == r ? [-1, -1] : [l, r - 1];
158
-
};
159
-
```
160
-
161
144
### **Go**
162
145
163
146
```go
@@ -204,6 +187,33 @@ impl Solution {
204
187
205
188
```ts
206
189
function searchRange(nums:number[], target:number):number[] {
Copy file name to clipboardexpand all lines: solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md
+11
Original file line number
Diff line number
Diff line change
@@ -102,6 +102,17 @@ int search(int left, int right) {
102
102
}
103
103
```
104
104
105
+
When doing binary search problems, you can follow the following routine:
106
+
107
+
1. Write out the loop condition $left < right$;
108
+
2. Inside the loop, you might as well write $mid = \lfloor \frac{left + right}{2} \rfloor$ first;
109
+
3. According to the specific problem, implement the $check()$ function (sometimes the logic is very simple, you can not define $check$), think about whether to use $right = mid$ (Template $1$) or $left = mid$ (Template $2$);
110
+
- If $right = mid$, then write the else statement $left = mid + 1$, and there is no need to change the calculation of $mid$, that is, keep $mid = \lfloor \frac{left + right}{2} \rfloor$;
111
+
- If $left = mid$, then write the else statement $right = mid - 1$, and add +1 when calculating $mid$, that is, $mid = \lfloor \frac{left + right + 1}{2} \rfloor$;
112
+
4. When the loop ends, $left$ equals $right$.
113
+
114
+
Note that the advantage of these two templates is that they always keep the answer within the binary search interval, and the value corresponding to the end condition of the binary search is exactly at the position of the answer. For the case that may have no solution, just check whether the $left$ or $right$ after the binary search ends satisfies the problem.
0 commit comments