题目描述
寻找 0 ~ n-1 中缺失的数字
思路:左边 $q[i] == i$ ,右边 $q[i] != i$ 。这就是二分性质的分界点
class Solution {
public:
int getMissingNumber(vector<int>& nums) {
if (nums.empty()) return 0;
int l = 0, r = nums.size() - 1;
while (l < r)
{
int mid = l + r >> 1;
if (nums[mid] == mid) l = mid + 1;
else r = mid;
}
if (nums[l] == l) return l + 1;
return l;
}
};