zoukankan      html  css  js  c++  java
  • Search in Rotated Sorted Array leetcode java

    题目:

    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在哪,自己画看看)。接下来就只需要按照这个特性继续写下去就好。以下就以伪代码方法来说明:

    1. 如果target比A[mid]值要小
    2.       如果A[mid]右边有序(A[mid]<A[high])
    3.             那么target肯定不在右边(target比右边的都得小),在左边找
    4.       如果A[mid]左边有序
    5.             那么比较target和A[low],如果target比A[low]还要小,证明target不在这一区,去右边找;反之,左边找。
    6. 如果target比A[mid]值要大
    7.      如果A[mid]左边有序(A[mid]>A[low])
    8.            那么target肯定不在左边(target比左边的都得大),在右边找
    9.      如果A[mid]右边有序
    10.            那么比较target和A[high],如果target比A[high]还要大,证明target不在这一区,去左边找;反之,右边找。

    以上实现代码如下所示:

     1    public int search(int [] A,int target){
     2        if(A==null||A.length==0)
     3          return -1;
     4         
     5        int low = 0;
     6        int high = A.length-1;
     7       
     8        while(low <= high){
     9            int mid = (low + high)/2;
    10            if(target < A[mid]){
    11                if(A[mid]<A[high])//right side is sorted
    12                  high = mid - 1;//target must in left side
    13                else
    14                  if(target<A[low])//target<A[mid]&&target<A[low]==>means,target cannot be in [low,mid] since this side is sorted
    15                     low = mid + 1;
    16                  else 
    17                     high = mid - 1;
    18            }else if(target > A[mid]){
    19                if(A[low]<A[mid])//left side is sorted
    20                  low = mid + 1;//target must in right side
    21                else
    22                  if(target>A[high])//right side is sorted. If target>A[high] means target is not in this side
    23                     high = mid - 1;
    24                  else
    25                     low = mid + 1;
    26            }else
    27              return mid;
    28        }
    29        
    30        return -1;
    31 }

     Reference:http://www.cnblogs.com/ider/archive/2012/04/01/binary_search.html

  • 相关阅读:
    严重: Parse error in application web.xml file at jndi:/localhost/ipws/WEBINF/web.xml java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml
    Failed to install .apk on device 'emulator5554': timeout解决方法
    java.lang.NoClassDefFoundError:org.jsoup.Jsoup
    Conversion to Dalvik format failed: Unable to execute dex:解决方法
    apache Digest: generating secret for digest authentication ...
    Description Resource Path Location Type Project has no default.properties file! Edit the project properties to set one.
    android service随机自启动
    MVC3 安装部署
    EF 4.3 CodeBased 数据迁移演练
    SQL Server 2008开启sa账户
  • 原文地址:https://www.cnblogs.com/springfor/p/3858140.html
Copyright © 2011-2022 走看看