Skip to content

Commit

Permalink
Update file
Browse files Browse the repository at this point in the history
  • Loading branch information
HarleysZhang committed Oct 7, 2021
1 parent 4727d9c commit e478b63
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions 3-data_structure-algorithm/binary_search/二分查找.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// 非递归实现
int BinarySearch(int a[], int n, int value){
int low = 0, high = n-1;
int mid = (high-low)/2;
int mid = (low+high)/2;
while( low <= high){
// mid = low+(high-low)/2; mid = low+((high-low)>>1)
mid = (high-low)/2;
mid = (low+high)/2;

if( a[mid] == value ){
return mid;
Expand Down
21 changes: 21 additions & 0 deletions 3-data_structure-algorithm/剑指offer题解.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,27 @@ public:
请必须使用时间复杂度为 O(log n) 的算法。
**解题方法**
二分查找法(非递归实现),查找结束如果没有相等值则返回 left,该值为插入位置
```cpp
class Solution {
public:
// 二分法+非递归实现
int searchInsert(vector<int>& nums, int target) {
int low = 0, high = nums.size()-1;
while( low <= high){
int mid = low+(high-low)/2; //mid = low+((high-low)>>1)
// int mid = (low+high)/2;
if( nums[mid] == target ) return mid;
else if (target < nums[mid]) high = mid - 1;
else low = mid + 1;
}
return low;
}
};
```
### 2,链表
Expand Down

0 comments on commit e478b63

Please sign in to comment.