while(i+1 < j)模板不可用的情形
标准while(left + 1 < right)的模板
//pre-processinng
...
left = 0, right = len - 1;
while (left + 1 < right) {
mid = left + (right - left) / 2;
if (nums[mid] <target) {
left = mid;
} else {
right = mid;
}
}
...
//left + 1 == right
//2 more candidates
//post-processingwhile (left + 1 < right)不可用的情形
while (left + 1 < right) 可用的前提:最终结果一定在nums[left] or nums[right]中间
如果第1条不能满足,可以考虑while(left <=right)的模板
Search Insert Position
Last updated