Description
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
cpp
class Solution { public: int search(vector& nums, int target) { int l = 0, h = nums.size() - 1, m; while(l <= h){ m = l + (h - l) / 2; if(target == nums[m]) return m; if(nums[m] <= nums[h]){ if(target < nums[m]) h = m - 1; else if(target <=nums[h]) l = m + 1; else h = m - 1; }else{ if(target > nums[m]) l = m + 1; else if(target >= nums[l]) h = m - 1; else l = m + 1; } } return -1; } };