Commit 44fe2a21 authored by anqiwa's avatar anqiwa 😲
Browse files

started binary search

parent 5c7a8da6
class Solution
{
public:
int searchInsert(vector<int> &nums, int target)
{
int l = 0;
int r = nums.size();
while (l < r)
{
int mid = l + (r - l) / 2; // [0,1] -> 0
if (nums[mid] < target)
{
l = mid + 1;
}
else if (nums[mid] > target)
{
r = mid;
}
else
{
return mid;
}
}
return r;
}
};
\ No newline at end of file
# 35. Search Insert Position
此题是binary search的模板题,很基础,初学者一定要牢记于心,闭着眼睛也要能默写出来
```cpp
int l = 0, r = nums.size();
while (l < r)
{
int mid = l + (r - l) / 2; // [0,1] -> 0
if (nums[mid] < target)
{
l = mid + 1;
}
else if (nums[mid] > target)
{
r = mid;
}
else
{
return mid;
}
}
```
\ No newline at end of file
......@@ -6,6 +6,14 @@
<br/><br/>
### <b/>分类目录</b>
[Binary Search]()
[35. Search Insert Position]()
[34. Find First and Last Position of Element in Sorted Array]()
[1060. Missing Element in Sorted Array]()
[Linked List](https://gitlab.eecs.umich.edu/anqiwa/myleetcodesolu/-/tree/master/Linked%20List)
[328.odd even linked list](https://gitlab.eecs.umich.edu/anqiwa/myleetcodesolu/-/tree/master/Linked%20List/328%20odd%20even%20Linked%20List) (M-)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment