class Solution {
public:
int bin_search(vector<int>& arr, int left, int right, int target) {
if(left>right) return -1;
if(left==right && arr[left]!=target) return -1;
int mid = (left+right) / 2;
if(arr[mid] == target) return mid;
if(arr[left]<=arr[mid]) {
if(target >= arr[left] && target <= arr[mid])
return bin_search(arr, left, mid, target);
else
return bin_search(arr, mid + 1, right, target);
} else {
if(target>=arr[mid] && target <= arr[right])
return bin_search(arr, mid, right, target);
else
return bin_search(arr, left, mid - 1, target);
}
}
int search(vector<int>& nums, int target) {
if(nums.size()==0) return -1;
return bin_search(nums, 0, nums.size() - 1, target);
}
};
Search in Rotated Sorted Array
最新推荐文章于 2018-09-18 20:48:05 发布