- 迄今为止,无论遇到的何种二分查找题目都可以抽象为如下形式:在一个具有false,false⋯false,true,true⋯truefalse,false \cdots false,true,true \cdots truefalse,false⋯false,true,true⋯true的数组中找到最后一个false对应的下标或者第一个truetruetrue对应的下标
- 在数组中找到target的结束位置,等价于找到最后一个小于等于target的值对应的下标,如果这个下标不满足条件,则target不存在
- 其余的问题均可用类似的思路进行抽象。
- 通用框架:定义函数f(x),x∈arrayf(x),x\in arrayf(x),x∈array,为从x到boolean值得映射。且如认为false<positivefalse < positivefalse<positive,则函数f(x)f(x)f(x)单调递增。
- low < high对应得搜索区间是[low,high)而不是[low,high]