`
ouqi
  • 浏览: 42164 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

[leetcode]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.

 

这题剑指offer上有类似的题 :旋转数组的最小值

关键点在于找到一个递增的序列,然后比较序列头尾和target的值。

大家可以想看看,如果A[i]<A[j]则区间[i,j]一定是递增的,画个二维坐标图比较好理解。

 

算法类似于常规的二分查找:

找到mid。

   1. 如果A[start]<=A[mid]说明start到mid一定是一组递增序列:如果A[start]>target,则明显往左走是不可能了,start = mid+1;如果A[start]<=target ,则需要比较递增序列的尾部A[mid]和target的大小,大于target ,则end = mid-1,否则start = mid+1;

    2. 如果A[mid]<=A[end]说明mid到end一定是一组递增序列:如果A[end]<target,则明显往右走是不可能了,end = mid-1;如果A[end]>=target,则需要比较比较递增序列的头部A[start]和target的大小,小于target,则start = mid+1; 大于则end = mid-1;

 

代码:

public static int search(int[] A, int target) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        if(A == null) return -1;
        int start = 0;
        int end = A.length-1;
        while(start<=end){
            int mid = start+(end-start)/2;
            if(A[mid] == target) return mid;
            if(A[start]<=A[mid]){
                 if(A[start]>target) start = mid+1; 
                 else{
                     if(A[mid]>target) end = mid-1;
                     else start = mid+1;
                 }
                  
            }
            else {//说明A[mid]<=A[end]
            	if(A[end]<target) end = mid-1;
                else{
                    if(A[mid]<target) start = mid+1;
                    else end = mid-1;
                }
          
            }
        }
        return -1;
    }

 

   

Search in Rotated Sorted Array II

 Follow up for "Search in Rotated Sorted Array":

What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

 

重复的情况下,A[mid]和A[start],A[end]相等的情况需要单独拿出来,当A[mid] 等于A[start]或者A[end]中一个时,根据另一个与A[mid]的大小关系,可以减少一半的范围。但是这三者相等的情况下,则只能顺序判断了。如3 3 3 1 3 和3 1 3 3 3 。

【更新】网上看到的。当这三者相等时,start++,找到不等的地方,继续二分即可。

代码:

 public boolean search(int[] A, int target) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        if(A ==  null ) return false;
        int start  = 0;
        int end = A.length-1;
        
        while(start<=end){
            int mid = start+(end-start)/2;
            if(A[mid] == target) return true;
            else if(A[start]<A[mid]){//start到mid非降序
                if(A[mid]<target) start = mid+1;
                else {
                    if(A[start]<=target) end = mid-1;
                    else start = mid+1;
                }
            }else if(A[start]>A[mid]){//mid到end非降序
                if(A[mid]>target) end = mid-1;
                else{
                    if(A[end]>=target) start = mid+1;
                    else end = mid-1;
                }
            }else{//A[start] = A[mid]!=target
                if(A[end]!=A[mid]) start = mid+1;
                else start++;
            }   
        }
        return false;
        
    }

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics