Skip to content

Commit 808ddd9

Browse files
authored
feat: add solutions to lc problem: No.704 (doocs#2518)
No.0704.Binary Search
1 parent 55f7ad1 commit 808ddd9

File tree

4 files changed

+62
-70
lines changed

4 files changed

+62
-70
lines changed

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

+24-25
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,18 @@
3737

3838
## 解法
3939

40-
### 方法一
40+
### 方法一:二分查找
41+
42+
我们定义二分查找的左边界 $left=0$,右边界 $right=n-1$。
43+
44+
每一次循环,我们计算中间位置 $mid=(left+right)/2$,然后判断 $nums[mid]$ 和 $target$ 的大小关系:
45+
46+
- 如果 $nums[mid] \geq target$,则说明 $target$ 在 $[left, mid]$ 之间,我们将 $right$ 更新为 $mid$;
47+
- 否则,说明 $target$ 在 $[mid+1, right]$ 之间,我们将 $left$ 更新为 $mid+1$。
48+
49+
当 $left \geq right$ 时,我们判断 $nums[left]$ 是否等于 $target$,如果等于则返回 $left$,否则返回 $-1$。
50+
51+
时间复杂度 $O(\log n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。
4152

4253
<!-- tabs:start -->
4354

@@ -153,31 +164,19 @@ var search = function (nums, target) {
153164
};
154165
```
155166

156-
<!-- tabs:end -->
157-
158-
### 方法二
159-
160-
<!-- tabs:start -->
161-
162-
```rust
163-
use std::cmp::Ordering;
164-
165-
impl Solution {
166-
fn binary_search(nums: Vec<i32>, target: i32, l: usize, r: usize) -> i32 {
167-
if l == r {
168-
return if nums[l] == target { l as i32 } else { -1 };
169-
}
170-
let mid = (l + r) >> 1;
171-
match nums[mid].cmp(&target) {
172-
Ordering::Less => Self::binary_search(nums, target, mid + 1, r),
173-
Ordering::Greater => Self::binary_search(nums, target, l, mid),
174-
Ordering::Equal => mid as i32,
167+
```cs
168+
public class Solution {
169+
public int Search(int[] nums, int target) {
170+
int left = 0, right = nums.Length - 1;
171+
while (left < right) {
172+
int mid = (left + right) >> 1;
173+
if (nums[mid] >= target) {
174+
right = mid;
175+
} else {
176+
left = mid + 1;
177+
}
175178
}
176-
}
177-
178-
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
179-
let r = nums.len() - 1;
180-
Self::binary_search(nums, target, 0, r)
179+
return nums[left] == target ? left : -1;
181180
}
182181
}
183182
```

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

+24-25
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,18 @@
3939

4040
## Solutions
4141

42-
### Solution 1
42+
### Solution 1: Binary Search
43+
44+
We define the left boundary of the binary search as $left=0$, and the right boundary as $right=n-1$.
45+
46+
In each iteration, we calculate the middle position $mid=(left+right)/2$, and then compare the size of $nums[mid]$ and $target$:
47+
48+
- If $nums[mid] \geq target$, it means that $target$ is in the interval $[left, mid]$, so we update $right$ to $mid$;
49+
- Otherwise, $target$ is in the interval $[mid+1, right]$, so we update $left$ to $mid+1$.
50+
51+
When $left \geq right$, we check if $nums[left]$ equals $target$. If it does, we return $left$, otherwise, we return $-1$.
52+
53+
The time complexity is $O(\log n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
4354

4455
<!-- tabs:start -->
4556

@@ -155,31 +166,19 @@ var search = function (nums, target) {
155166
};
156167
```
157168

158-
<!-- tabs:end -->
159-
160-
### Solution 2
161-
162-
<!-- tabs:start -->
163-
164-
```rust
165-
use std::cmp::Ordering;
166-
167-
impl Solution {
168-
fn binary_search(nums: Vec<i32>, target: i32, l: usize, r: usize) -> i32 {
169-
if l == r {
170-
return if nums[l] == target { l as i32 } else { -1 };
171-
}
172-
let mid = (l + r) >> 1;
173-
match nums[mid].cmp(&target) {
174-
Ordering::Less => Self::binary_search(nums, target, mid + 1, r),
175-
Ordering::Greater => Self::binary_search(nums, target, l, mid),
176-
Ordering::Equal => mid as i32,
169+
```cs
170+
public class Solution {
171+
public int Search(int[] nums, int target) {
172+
int left = 0, right = nums.Length - 1;
173+
while (left < right) {
174+
int mid = (left + right) >> 1;
175+
if (nums[mid] >= target) {
176+
right = mid;
177+
} else {
178+
left = mid + 1;
179+
}
177180
}
178-
}
179-
180-
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
181-
let r = nums.len() - 1;
182-
Self::binary_search(nums, target, 0, r)
181+
return nums[left] == target ? left : -1;
183182
}
184183
}
185184
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Solution {
2+
public int Search(int[] nums, int target) {
3+
int left = 0, right = nums.Length - 1;
4+
while (left < right) {
5+
int mid = (left + right) >> 1;
6+
if (nums[mid] >= target) {
7+
right = mid;
8+
} else {
9+
left = mid + 1;
10+
}
11+
}
12+
return nums[left] == target ? left : -1;
13+
}
14+
}

solution/0700-0799/0704.Binary Search/Solution2.rs

-20
This file was deleted.

0 commit comments

Comments
 (0)