Skip to content

Commit

Permalink
Merge pull request #477 from xybh-l/patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
azl397985856 authored Jan 11, 2021
2 parents de25c9c + 8417aee commit 1c4beee
Showing 1 changed file with 62 additions and 7 deletions.
69 changes: 62 additions & 7 deletions 91/binary-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,24 @@ function binarySearch(nums, target) {

##### C++

暂时空缺,欢迎 [PR](https://github.com/leetcode-pp/leetcode-cheat/pulls)

```cpp
int binarySearch(vector<int>& nums, int target){
if(nums.size() == 0)
return -1;

int left = 0, right = nums.size() - 1;
while(left <= right){
int mid = left + ((right - left) >> 1);
if(nums[mid] == target){ return mid; }
// 搜索区间变为 [mid+1, right]
else if(nums[mid] < target)
left = mid + 1;
// 搜索区间变为 [left, mid - 1]
else
right = mid - 1;
}
return -1;
}
```
### 寻找最左边的满足条件的值
Expand Down Expand Up @@ -258,10 +272,31 @@ function binarySearchLeft(nums, target) {

##### C++

暂时空缺,欢迎 [PR](https://github.com/leetcode-pp/leetcode-cheat/pulls)

```cpp

int binarySearchLeft(vector<int>& nums, int target) {
// 搜索区间为 [left, right]
int left = 0, right = nums.size() - 1;
while (left <= right) {
int mid = left + ((right - left) >> 1);
if (nums[mid] == target) {
// 收缩右边界
right = mid - 1;
}
if (nums[mid] < target) {
// 搜索区间变为 [mid+1, right]
left = mid + 1;
}
if (nums[mid] > target) {
// 搜索区间变为 [left, mid-1]
right = mid - 1;
}
}
// 检查是否越界
if (left >= nums.size() || nums[left] != target)
return -1;
return left;
}
```
### 寻找最右边的满足条件的值
Expand Down Expand Up @@ -366,10 +401,30 @@ function binarySearchRight(nums, target) {

##### C++

暂时空缺,欢迎 [PR](https://github.com/leetcode-pp/leetcode-cheat/pulls)

```cpp

int binarySearchRight(vector<int>& nums, int target) {
// 搜索区间为 [left, right]
int left = 0, right = nums.size() - 1;
while (left <= right) {
int mid = left + ((right - left) >> 1);
if (nums[mid] == target) {
// 收缩左边界
left = mid + 1;
}
if (nums[mid] < target) {
// 搜索区间变为 [mid+1, right]
left = mid + 1;
}
if (nums[mid] > target) {
// 搜索区间变为 [left, mid-1]
right = mid - 1;
}
}
// 检查是否越界
if (right < 0 || nums[right] != target)
return -1;
return right;
}
```
### 寻找最左插入位置
Expand Down

0 comments on commit 1c4beee

Please sign in to comment.