*Search in Rotated Sorted Array

题目:

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.

题解:

这道题是一道常见的二分查找法的变体题。

要解决这道题,需要明确rotated sorted array的特性,那么就是至少有一侧是排好序的(无论pivot在哪,自己画看看)。接下来就只需要按照这个特性继续写下去就好。以下是思路:

Take “4 5 6 7 0 1 2″ as an example. The mid entry is 7. We can compare it with the first entry. If the first entry is smaller than the mid entry, then the first half (from 4 to 7) must be in strictly increasing order. So we can compare target with the first entry and the mid entry, then we can decide if the target is in this half or not. If the first entry is larger than the mid entry, then the second half (fron 7 to 2) is in strictly increasing order. We can compare the target with them. Using this algorithm, every time we can throw half of the array.

public class Solution {
    public int search(int[] nums, int target) 
    {
        int left = 0;
        int right = nums.length-1;
        
        while(left<=right)
        {
            int mid = left + (right-left)/2;
            if(nums[mid]==target) return mid;
            if(nums[mid]<nums[right])   //half right is sorted
            {
                if(target > nums[mid]&&target <= nums[right])
                {
                    left = mid+1;
                }
                else
                {
                    right = mid-1;
                }
            }
            else  //half left is sorted 
            {
                if(target >= nums[left] && target< nums[mid])
                {
                    right = mid-1;
                }
                else
                {
                    left = mid+1;
                }
            
            }
        }
        return -1;
    }
}

Complexity

The complexity is O(log n), which is similar to binary search.

Follow up

Find the rotation pivot.

Solution from leetcode.com.

 1 int FindSortedArrayRotation(int A[], int N) {
 2   int L = 0;
 3   int R = N - 1;
 4   
 5   while (A[L] > A[R]) {
 6     int M = L + (R - L) / 2;
 7     if (A[M] > A[R])
 8       L = M + 1;
 9     else
10       R = M;
11   }
12   return L;
13 }

For details:  http://leetcode.com/2010/04/searching-element-in-rotated-array.html.

九章算法的模板解法:

 1 /**
 2  * 本代码由九章算法编辑提供。没有版权欢迎转发。
 3  * - 九章算法致力于帮助更多中国人找到好的工作,教师团队均来自硅谷和国内的一线大公司在职工程师。
 4  * - 现有的面试培训课程包括:九章算法班,系统设计班,BAT国内班
 5  * - 更多详情请见官方网站:http://www.jiuzhang.com/
 6  */
 7 
 8 public class Solution {
 9     public int search(int[] A, int target) {
10         if (A == null || A.length == 0) {
11             return -1;
12         }
13 
14         int start = 0;
15         int end = A.length - 1;
16         int mid;
17         
18         while (start + 1 < end) {
19             mid = start + (end - start) / 2;
20             if (A[mid] == target) {
21                 return mid;
22             }
23             if (A[start] < A[mid]) {
24                 // situation 1, red line
25                 if (A[start] <= target && target <= A[mid]) {
26                     end = mid;
27                 } else {
28                     start = mid;
29                 }
30             } else {
31                 // situation 2, green line
32                 if (A[mid] <= target && target <= A[end]) {
33                     start = mid;
34                 } else {
35                     end = mid;
36                 }
37             }
38         } // while
39         
40         if (A[start] == target) {
41             return start;
42         }
43         if (A[end] == target) {
44             return end;
45         }
46         return -1;
47     }
48 }

Reference: 

http://www.lifeincode.net/programming/leetcode-search-in-rotated-sorted-array-java/

http://www.cnblogs.com/springfor/p/3858140.html

http://leetcode.com/2010/04/searching-element-in-rotated-array.html.

原文地址:https://www.cnblogs.com/hygeia/p/4635992.html