数组中数值和下标相等的元素
论二分法的重要性!!!
class Solution {
public int getNumberSameAsIndex(int[] nums) {
int n = nums.length;
if (n == 0)
return -1;
int l = 0, r = n - 1;
while (l < r) {
int mid = l + r >> 1;
if (nums[mid] >= mid)
r = mid;
if (nums[mid] < mid)
l = mid + 1;
}
if (nums[l] == l)
return l;
else
return -1;
}
}