采用二分法,当下标对应的值大于下标时,l = mid,否则,r = mid - 1
class Solution {
public:
int getNumberSameAsIndex(vector<int>& nums) {
int l = 0, r = nums.size()-1;
int res = 0;
while(l <= r)
{
res = l + r + 1 >> 1;
if(res > nums[res])
l = res;
else
r = res - 1;
if(res == nums[res])
return res;
}
return -1;
}
};