思路:先找到断点。估计还有更快的,不知道为什么,实在是不愿意去想这道题了
class Solution { public: int search(vector<int>& nums, int target) { int i,index=0; for(i=1;i<nums.size();i++) if(nums[i]<nums[i-1]) { index=i; break; } if(i==nums.size()) search_2(nums,0,nums.size()-1,target); if(target>=nums[0] && target<=nums[index-1]) search_2(nums,0,index-1,target); else search_2(nums,index,nums.size()-1,target); } int search_2(vector<int>& A, int L, int R, int target) { while(L<=R) { int mid=(L+R)>>1; if(A[mid]>target) { R=mid-1; } else if(A[mid]<target) { L=mid+1; } else return mid; } return -1; } };
1 minute ago | Accepted | 4 ms | cpp |