Problem
Suppose a sorted array 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.
Ideas
- http://www.cnblogs.com/TenosDoIt/p/3465240.html
- target < A[mid]时,搜索后半段必须同时满足以下条件
- A[left] > A[right] // 表明搜索的边界落在rotated subarray
- target <= A[right] // target 可能在unrotated的数组的前半部分
- A[right] < A[mid] // A[mid]在unrotated数组的后半部分,换言之,rotate的点靠近left
- target > A[mid]时,搜索前半段
- A[left] > A[right] // 表明搜索的边界落在rotated subarray
- target > A[right] // 表明target在unrotated数组的后半部分
- A[mid] < A[right] // 表明A[mid]在unrotated数组的前半部分,换言之,rotate的点靠近right
No comments:
Post a Comment