时间复杂度:基本为T(n) = T(n/2) + O(1) = T(n/4) + 2O(1) = T(1) + O(logn) = O(logn)
1. 二分模版
Find any/first/last position of target in nums[]
int start = 0, end = nums.length - 1;
while (start +1 < end) {
int mid = start + (end - start) / 2; (avoid overflow)
if(nums[mid] == target) {}
}
- XXOO
Find first/last 满足某条件的position
eg. Search in a big sorted array
int index = 1
index * 2 增大至 > target, 找到end = index - 1eg. Find minimum in a rotated sorted array
target = 最后一个数,再二分eg. Search a 2D matrix
常规: search a value in 严格递增的 matrix
在第一列二分,找到target所在的row
在该行二分,找到target所在的column
变形:找到出现的次数,行、列分别递增
(非二分法)
从左下角/右上角出发,分别比较target,每次可以排除一行/一列
遍历matrix,找到count
eg。
- Half Half 保留一半