難度:medium
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.
思路:假設有一個排序的按未知的旋轉軸旋轉的數組(比如,0 1 2 4 5 6 7 可能成為4 5 6 7 0 1 2)。給定一個目标值進行搜尋,如果在數組中找到目标值傳回數組中的索引位置,否則傳回-1。
如果用周遊序列的方法,或者雙支針的方法從頭尾雙向查找都會逾時。顯然需用兩分法縮小查找範圍,然後雙支針查找。