二分
C++ 代码
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) return mid;
if(nums[mid] < mid) l = mid + 1;
else r = mid - 1;
}
if(nums[l] != l) return -1;
return l;
}
};