题目描述
blablabla
样例
blablabla
算法1
(二分法) $O(logn)$
blablabla
时间复杂度分析:blablabla
C++ 代码
class Solution {
public:
int getNumberSameAsIndex(vector<int>& nums) {
int n = nums.size();
int l = 0, r = n-1;
while(l <= r){
int mid = (l+r)/2;
if (nums[mid] == mid) return mid;
else if (nums[mid] < mid ) l = mid + 1;
else r = mid - 1;
}
return -1;
}
};