/* 题目: 求单调递增数组中,数值与下标相等的任意数字。 */ /* 思路: 二分法。 */ #include<iostream> #include<cstring> #include<vector> #include<algorithm> #include<map> using namespace std; int getNumberSameAsIndex(vector<int> &A, int n){ if(n <= 0 || A.empty()) return -1; int start = 0; int end = n - 1; while(start <= end){ int middle = (start + end) / 2; if(A[middle] < middle){ start = middle + 1; }else if(A[middle] > middle){ end = middle - 1; }else{ return middle; } } return -1; } int main(){ vector<int> a = {-3,-1,1,3,5}; cout<<getNumberSameAsIndex(a,5); }