Skip to content

Commit 2828398

Browse files
authored
Update README_EN.md
1 parent 5e6951e commit 2828398

File tree

1 file changed

+74
-79
lines changed

1 file changed

+74
-79
lines changed

solution/0700-0799/0704.Binary Search/README_EN.md

Lines changed: 74 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@ tags:
5656

5757
### Solution 1: Binary Search
5858

59-
We define the left boundary of the binary search as $left=0$, and the right boundary as $right=n-1$.
59+
We define the left boundary $l=0$ and the right boundary $r=n-1$ for binary search.
6060

61-
In each iteration, we calculate the middle position $mid=(left+right)/2$, and then compare the size of $nums[mid]$ and $target$:
61+
In each iteration, we calculate the middle position $\text{mid}=(l+r)/2$, then compare the size of $\text{nums}[\text{mid}]$ and $\text{target}$.
6262

63-
- If $nums[mid] \geq target$, it means that $target$ is in the interval $[left, mid]$, so we update $right$ to $mid$;
64-
- Otherwise, $target$ is in the interval $[mid+1, right]$, so we update $left$ to $mid+1$.
63+
- If $\text{nums}[\text{mid}] \geq \text{target}$, it means $\text{target}$ is in the left half, so we move the right boundary $r$ to $\text{mid}$;
64+
- Otherwise, it means $\text{target}$ is in the right half, so we move the left boundary $l$ to $\text{mid}+1$.
6565

66-
When $left \geq right$, we check if $nums[left]$ equals $target$. If it does, we return $left$, otherwise, we return $-1$.
66+
The loop ends when $l<r$, at this point $\text{nums}[l]$ is the target value we are looking for. If $\text{nums}[l]=\text{target}$, return $l$; otherwise, return $-1$.
6767

68-
The time complexity is $O(\log n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
68+
The time complexity is $O(\log n)$, where $n$ is the length of the array $\text{nums}$. The space complexity is $O(1)$.
6969

7070
<!-- tabs:start -->
7171

@@ -74,31 +74,31 @@ The time complexity is $O(\log n)$, where $n$ is the length of the array $nums$.
7474
```python
7575
class Solution:
7676
def search(self, nums: List[int], target: int) -> int:
77-
left, right = 0, len(nums) - 1
78-
while left < right:
79-
mid = (left + right) >> 1
77+
l, r = 0, len(nums) - 1
78+
while l < r:
79+
mid = (l + r) >> 1
8080
if nums[mid] >= target:
81-
right = mid
81+
r = mid
8282
else:
83-
left = mid + 1
84-
return left if nums[left] == target else -1
83+
l = mid + 1
84+
return l if nums[l] == target else -1
8585
```
8686

8787
#### Java
8888

8989
```java
9090
class Solution {
9191
public int search(int[] nums, int target) {
92-
int left = 0, right = nums.length - 1;
93-
while (left < right) {
94-
int mid = (left + right) >> 1;
92+
int l = 0, r = nums.length - 1;
93+
while (l < r) {
94+
int mid = (l + r) >> 1;
9595
if (nums[mid] >= target) {
96-
right = mid;
96+
r = mid;
9797
} else {
98-
left = mid + 1;
98+
l = mid + 1;
9999
}
100100
}
101-
return nums[left] == target ? left : -1;
101+
return nums[l] == target ? l : -1;
102102
}
103103
}
104104
```
@@ -109,15 +109,16 @@ class Solution {
109109
class Solution {
110110
public:
111111
int search(vector<int>& nums, int target) {
112-
int left = 0, right = nums.size() - 1;
113-
while (left < right) {
114-
int mid = left + right >> 1;
115-
if (nums[mid] >= target)
116-
right = mid;
117-
else
118-
left = mid + 1;
112+
int l = 0, r = nums.size() - 1;
113+
while (l < r) {
114+
int mid = (l + r) >> 1;
115+
if (nums[mid] >= target) {
116+
r = mid;
117+
} else {
118+
l = mid + 1;
119+
}
119120
}
120-
return nums[left] == target ? left : -1;
121+
return nums[l] == target ? l : -1;
121122
}
122123
};
123124
```
@@ -126,46 +127,59 @@ public:
126127
127128
```go
128129
func search(nums []int, target int) int {
129-
left, right := 0, len(nums)-1
130-
for left < right {
131-
mid := (left + right) >> 1
130+
l, r := 0, len(nums)-1
131+
for l < r {
132+
mid := (l + r) >> 1
132133
if nums[mid] >= target {
133-
right = mid
134+
r = mid
134135
} else {
135-
left = mid + 1
136+
l = mid + 1
136137
}
137138
}
138-
if nums[left] == target {
139-
return left
139+
if nums[l] == target {
140+
return l
140141
}
141142
return -1
142143
}
143144
```
144145

146+
#### TypeScript
147+
148+
```ts
149+
function search(nums: number[], target: number): number {
150+
let [l, r] = [0, nums.length - 1];
151+
while (l < r) {
152+
const mid = (l + r) >> 1;
153+
if (nums[mid] >= target) {
154+
r = mid;
155+
} else {
156+
l = mid + 1;
157+
}
158+
}
159+
return nums[l] === target ? l : -1;
160+
}
161+
```
162+
145163
#### Rust
146164

147165
```rust
148-
use std::cmp::Ordering;
149-
150166
impl Solution {
151167
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
152-
let mut l = 0;
153-
let mut r = nums.len();
168+
let mut l: usize = 0;
169+
let mut r: usize = nums.len() - 1;
154170
while l < r {
155171
let mid = (l + r) >> 1;
156-
match nums[mid].cmp(&target) {
157-
Ordering::Less => {
158-
l = mid + 1;
159-
}
160-
Ordering::Greater => {
161-
r = mid;
162-
}
163-
Ordering::Equal => {
164-
return mid as i32;
165-
}
172+
if nums[mid] >= target {
173+
r = mid;
174+
} else {
175+
l = mid + 1;
166176
}
167177
}
168-
-1
178+
if nums[l] == target {
179+
l as i32
180+
} else {
181+
-1
182+
}
169183
}
170184
}
171185
```
@@ -179,53 +193,34 @@ impl Solution {
179193
* @return {number}
180194
*/
181195
var search = function (nums, target) {
182-
let left = 0;
183-
let right = nums.length - 1;
184-
while (left < right) {
185-
const mid = (left + right) >> 1;
196+
let [l, r] = [0, nums.length - 1];
197+
while (l < r) {
198+
const mid = (l + r) >> 1;
186199
if (nums[mid] >= target) {
187-
right = mid;
200+
r = mid;
188201
} else {
189-
left = mid + 1;
202+
l = mid + 1;
190203
}
191204
}
192-
return nums[left] == target ? left : -1;
205+
return nums[l] === target ? l : -1;
193206
};
194207
```
195208

196-
#### TypeScript
197-
198-
```ts
199-
function search(nums: number[], target: number): number {
200-
let [l, r] = [0, nums.length - 1];
201-
202-
while (l <= r) {
203-
const mid = (l + r) >> 1;
204-
205-
if (nums[mid] === target) return mid;
206-
if (nums[mid] < target) l = mid + 1;
207-
else r = mid - 1;
208-
}
209-
210-
return -1;
211-
}
212-
```
213-
214209
#### C#
215210

216211
```cs
217212
public class Solution {
218213
public int Search(int[] nums, int target) {
219-
int left = 0, right = nums.Length - 1;
220-
while (left < right) {
221-
int mid = (left + right) >> 1;
214+
int l = 0, r = nums.Length - 1;
215+
while (l < r) {
216+
int mid = (l + r) >> 1;
222217
if (nums[mid] >= target) {
223-
right = mid;
218+
r = mid;
224219
} else {
225-
left = mid + 1;
220+
l = mid + 1;
226221
}
227222
}
228-
return nums[left] == target ? left : -1;
223+
return nums[l] == target ? l : -1;
229224
}
230225
}
231226
```

0 commit comments

Comments
 (0)