标签:
class Solution { public: int search(vector<int>& nums, int target) { return rotate_search(nums,0,nums.size()-1,target); } int rotate_search(vector<int>& nums,int l,int r, int target){ int left=l; int right=r; int mid = (left + right)/2; if(left > right) return -1; if(left == right) return target==nums[mid]?right:-1; if(nums[mid] >= nums[left]){ { cout<<"1"<<left<<" "<<mid; int temp =binary_search(nums,left, mid, target); if(temp != -1) return temp; } {cout<<"2";return rotate_search(nums,mid+1,right,target); } } else{ { cout<<"3:"<<mid<<" "<<right; int temp =binary_search(nums, mid, right, target); if(temp != -1) return temp; } { cout<<"4";return rotate_search(nums,left,mid-1,target); } } return -1; } int binary_search(vector<int>& nums,int l,int r, int target){ int left=l; int right=r; int mid; mid = (left + right)/2; if(left > right) return -1; if(left == right) return nums[mid]==target?left:-1; if(nums[mid] == target) return mid; else if(nums[mid] < target) return binary_search(nums,mid+1,r,target); return binary_search(nums,l,mid-1,target); } };
Search in Rotated Sorted Array
标签:
原文地址:http://www.cnblogs.com/julie-yang/p/4696661.html