Leetcode Binary Search & Divide and Conquer

C++

Posted by PZ on March 5, 2020

img

img

  • Template寻找最小的值满足 g(m)—二分核心思想 — 记住

img


Leetcode 35 Search Insert Position

int m = 0 out of the while loop save more memory usage

img

二分法总结

https://www.cnblogs.com/grandyang/p/6854825.html 二分搜索总结 如果需要对边缘元素 Array[r] 进行判断就要用闭合区间[l,r]

Leetcode 33, 81 Search in Rotated Sorted Array

通过二分法判断哪个部分是有序的 因为需要比较nums[r],所以用闭合区间

search in Rotated Sorted Array 2

if (nums[mid] == nums[r]) r --;

img

img

  • 二分法查找最小值所在的index 这个只针对 rotated stored array

  • 当需要访问right 的时候就需要用闭合区间

img


Leetcode 852 Peak Index in Mountain Array

using the template for upper bound

A[m] > A[m+1]

img

Leetcode 153 Find Minimum in Rotated Sorted Array

##### Divide and Conquer && Binary Search — think both of them

img

img

img

### Leetcode 154 Find Minimum in Rotated Sorted Array II

img

Leetcode 162 Find Peak Element

时刻保持l会大于左边值,r会大于右边值

img

img