思路:每次取中间元素,一定有一半有序,另一半部分有序,有序的部分进行二分查找,部分有序的部分递归继续处理。
class Solution {public: int pos = -1; int middleSearch(int left, int right,vector & nums, int target){ //二分查找 if(left > right || right > nums.size() - 1 || left < 0)return -1; if(nums[left] == target)return left; int mid = (left + right) / 2; if(nums[mid] == target)return mid; if(target>nums[mid]) return middleSearch(mid+1,right,nums,target); else return middleSearch(left,mid - 1,nums,target); } void mixSearch(int left, int right, vector & nums, int target){ //混合处理 if(left > right || right > nums.size() - 1 || left < 0) return; if(nums[left] == target) { pos = left; return; } int ans; int middle = (left + right) / 2; if(nums[middle] > nums[right]) { ans = middleSearch(left,middle,nums,target); if(ans == -1) mixSearch(middle+1,right,nums,target); else{ pos = ans; return; } } else{ ans = middleSearch(middle,right,nums,target); if(ans == -1) mixSearch(left,middle - 1,nums,target); else{ pos = ans; return; } } } int search(vector & nums, int target) { mixSearch(0,nums.size() - 1,nums,target); return pos; }};
写的冗余了点,附上简洁的代码:
class Solution {public: int search(vector & nums, int target) { int l = 0, r = nums.size() - 1; while(l <= r){ int mid = l + (r - l) / 2; if(nums[mid] == target) return mid; if(nums[mid] > nums[r]){ if(target > nums[mid] || target <= nums[r]) l = mid + 1; // condition for pick right side else r = mid - 1; // else, pick left side }else{ if(target <= nums[r] && target > nums[mid]) l = mid + 1; // condition for pick right side else r = mid - 1; // else, pick left side } } return -1; }};