二分查找不变量(lc 35)
target 位于 [l,r];
r=n-1;
while (l <= r) { //l == r 时[l,r]仍然成立
m = l + (r - l) / 2;
if (a[m] = target) return m;
else if (a[m] < target) l = m + 1;
else r = m - 1;
}
return l;
target 位于[l,r)
r=n;
while (l < r) { //[l,r)
m = l + (r - l) / 2;
if (a[m] = target) return m;
else if (a[m] < target) l = m+1;
else r = m;
}
return l;