直接二分,时间复杂度(logn)
class Solution {
public:
int getNumberSameAsIndex(vector<int>& nums) {
int l=0,r=nums.size()-1;
while(l<r)
{
int mid=l+r>>1;
if(nums[mid]>=mid) r=mid;
else l=mid+1;
}
if(nums[l]!=l) return -1;
else return nums[r];
}
};