LeetCodeSearch in Rotated Sorted Array——旋转有序数列找目标值

Posted 鸭子船长

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCodeSearch 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.

【二分思路】

分情况讨论,数组可能有以下三种情况:

技术分享

然后,再看每一种情况中,target在左边还是在右边,其中第一种情况还可以直接判断target有可能不在数组范围内。

 1 public class Solution {
 2     public int search(int[] A, int target) {
 3         int len = A.length;
 4         if (len == 0) return -1;
 5         return binarySearch(A, 0, len-1, target);
 6     }
 7     
 8     public int binarySearch(int[] A, int left, int right, int target) {
 9         if (left > right) return -1;
10         
11         int mid = (left + right) / 2;
12         if (A[left] == target) return left;
13         if (A[mid] == target) return mid;
14         if (A[right] == target) return right;
15         
16         //图示情况一
17         if (A[left] < A[right]) { 
18             if (target < A[left] || target > A[right]) {    //target不在数组范围内
19                 return -1;
20             } else if (target < A[mid]) {                   //target在左边
21                 return binarySearch(A, left+1, mid-1, target);
22             } else {                                        //target在右边
23                 return binarySearch(A, mid+1, right-1, target);
24             }
25         } 
26         //图示情况二
27         else if (A[left] < A[mid]) { 
28             if (target > A[left] && target < A[mid]) {      //target在左边
29                 return binarySearch(A, left+1, mid-1, target);
30             } else {                                        //target在右边
31                 return binarySearch(A, mid+1, right-1, target);
32             }
33         } 
34         //图示情况三
35         else { 
36             if (target > A[mid] && target < A[right]) {     //target在右边
37                 return binarySearch(A, mid+1, right-1, target);
38             } else{                                         //target在左边
39                 return binarySearch(A, left+1, mid-1, target);
40             }
41         }
42     }
43 }

 

我的解法,不是最优解

 1 class Solution {
 2 public:
 3     int search(int A[], int n, int target) {
 4         if(A==NULL||n<1) return -1;
 5         int index=0;
 6         for(int i=1;i<n;i++){
 7             if(A[i-1]>A[i]){
 8                 index=i;
 9                 break;
10             }
11         }
12         int left,right;
13         if(target>=A[0]&&target<=A[index-1]){
14             left=0;
15             right=index-1;
16         }else if(target>=A[index]&&target<=A[n-1]){
17             left=index;
18             right=n-1;
19         }else
20             return -1;
21         while(left<=right){
22             int mid=(left+right)/2;
23             if(target==A[left])
24                 return left;
25             if(target==A[right])
26                 return right;
27             if(target==A[mid])
28                 return mid;
29             if(target>A[left]&&target<A[mid]){
30                 left++;
31                 right=mid-1;
32             }else{
33                 right--;
34                 left=mid+1;
35             }
36         }
37         return -1;
38     }
39 };

 

以上是关于LeetCodeSearch in Rotated Sorted Array——旋转有序数列找目标值的主要内容,如果未能解决你的问题,请参考以下文章

LeetCodeSearch in Rotated Sorted Array——旋转有序数列找目标值

[Lintcode]62. Search in Rotated Sorted Array/[Leetcode]33. Search in Rotated Sorted Array

Search in Rotated Sorted Array

Search in Rotated Sorted Array

33. Search in Rotated Sorted Array *HARD*

33. Search in Rotated Sorted Array