11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code to find an element in the array. You may assume that the array was originally sorted in increasing order.
EXAMPLE
Input: find 5 in {15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14}
Output: 8 (the index of 5 in the array)
LeetCode上的原題,請參見我之前的部落格Search in Rotated Sorted Array 在旋轉有序陣列中搜尋和Search in Rotated Sorted Array II 在旋轉有序陣列中搜尋之二。
class Solution { public: int search(vector<int> nums, int target) { int left = 0, right = nums.size() - 1; while (left <= right) { int mid = left + (right - left ) / 2; if (nums[mid] == target) return mid; else if (nums[mid] < nums[right]) { if (nums[mid] < target && nums[right] >= target) left = mid + 1; else right = mid - 1; } else if (nums[mid] > nums[right]) { if (nums[left] <= target && nums[mid] > target) right = mid - 1; else left = mid + 1; } else --right; } return -1; } };