二分法
class Solution { public: int getNumberSameAsIndex(vector<int>& nums) { int l = 0,r=nums.size()-1; while(l < r) { //cout<< "--1--"<<" "<<"l="<<l<<" "<<"r="<<r<<endl; int mid = l + r >> 1; //cout<<"mid="<<mid<<endl; if(nums[mid] - mid >=0) r=mid; else l = mid +1; //cout<< "--2--"<<" "<<"l="<<l<<" "<<"r="<<r<<endl; } //cout<<"跳出while"<<endl; if(nums[r] -r == 0) return r; return -1; } };