Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.704 #2518

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 24 additions & 25 deletions solution/0700-0799/0704.Binary Search/README.md
Original file line number Diff line number Diff line change
@@ -37,7 +37,18 @@

## 解法

### 方法一
### 方法一:二分查找

我们定义二分查找的左边界 $left=0$,右边界 $right=n-1$。

每一次循环,我们计算中间位置 $mid=(left+right)/2$,然后判断 $nums[mid]$ 和 $target$ 的大小关系:

- 如果 $nums[mid] \geq target$,则说明 $target$ 在 $[left, mid]$ 之间,我们将 $right$ 更新为 $mid$;
- 否则,说明 $target$ 在 $[mid+1, right]$ 之间,我们将 $left$ 更新为 $mid+1$。

当 $left \geq right$ 时,我们判断 $nums[left]$ 是否等于 $target$,如果等于则返回 $left$,否则返回 $-1$。

时间复杂度 $O(\log n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

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

<!-- tabs:end -->

### 方法二

<!-- tabs:start -->

```rust
use std::cmp::Ordering;

impl Solution {
fn binary_search(nums: Vec<i32>, target: i32, l: usize, r: usize) -> i32 {
if l == r {
return if nums[l] == target { l as i32 } else { -1 };
}
let mid = (l + r) >> 1;
match nums[mid].cmp(&target) {
Ordering::Less => Self::binary_search(nums, target, mid + 1, r),
Ordering::Greater => Self::binary_search(nums, target, l, mid),
Ordering::Equal => mid as i32,
```cs
public class Solution {
public int Search(int[] nums, int target) {
int left = 0, right = nums.Length - 1;
while (left < right) {
int mid = (left + right) >> 1;
if (nums[mid] >= target) {
right = mid;
} else {
left = mid + 1;
}
}
}

pub fn search(nums: Vec<i32>, target: i32) -> i32 {
let r = nums.len() - 1;
Self::binary_search(nums, target, 0, r)
return nums[left] == target ? left : -1;
}
}
```
49 changes: 24 additions & 25 deletions solution/0700-0799/0704.Binary Search/README_EN.md
Original file line number Diff line number Diff line change
@@ -39,7 +39,18 @@

## Solutions

### Solution 1
### Solution 1: Binary Search

We define the left boundary of the binary search as $left=0$, and the right boundary as $right=n-1$.

In each iteration, we calculate the middle position $mid=(left+right)/2$, and then compare the size of $nums[mid]$ and $target$:

- If $nums[mid] \geq target$, it means that $target$ is in the interval $[left, mid]$, so we update $right$ to $mid$;
- Otherwise, $target$ is in the interval $[mid+1, right]$, so we update $left$ to $mid+1$.

When $left \geq right$, we check if $nums[left]$ equals $target$. If it does, we return $left$, otherwise, we return $-1$.

The time complexity is $O(\log n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.

<!-- tabs:start -->

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

<!-- tabs:end -->

### Solution 2

<!-- tabs:start -->

```rust
use std::cmp::Ordering;

impl Solution {
fn binary_search(nums: Vec<i32>, target: i32, l: usize, r: usize) -> i32 {
if l == r {
return if nums[l] == target { l as i32 } else { -1 };
}
let mid = (l + r) >> 1;
match nums[mid].cmp(&target) {
Ordering::Less => Self::binary_search(nums, target, mid + 1, r),
Ordering::Greater => Self::binary_search(nums, target, l, mid),
Ordering::Equal => mid as i32,
```cs
public class Solution {
public int Search(int[] nums, int target) {
int left = 0, right = nums.Length - 1;
while (left < right) {
int mid = (left + right) >> 1;
if (nums[mid] >= target) {
right = mid;
} else {
left = mid + 1;
}
}
}

pub fn search(nums: Vec<i32>, target: i32) -> i32 {
let r = nums.len() - 1;
Self::binary_search(nums, target, 0, r)
return nums[left] == target ? left : -1;
}
}
```
14 changes: 14 additions & 0 deletions solution/0700-0799/0704.Binary Search/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Solution {
public int Search(int[] nums, int target) {
int left = 0, right = nums.Length - 1;
while (left < right) {
int mid = (left + right) >> 1;
if (nums[mid] >= target) {
right = mid;
} else {
left = mid + 1;
}
}
return nums[left] == target ? left : -1;
}
}
20 changes: 0 additions & 20 deletions solution/0700-0799/0704.Binary Search/Solution2.rs

This file was deleted.

Loading