【LeetCode】Search in Rotated Sorted Array && Search in Rotated Sorted Array II

1、Search in Rotated Sorted Array  
Total Accepted: 9880 Total Submissions: 35305 My Submissions
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.
2、Search in Rotated Sorted Array II
Total Accepted: 6430 Total Submissions: 21681 My Submissions
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.
这两道题应该是二分搜索的变形题,但是变化不大,还有思路可循。
最简单也最容易想到的思路,暴力搜索,但是这个复杂度太高,需要O(N),扫描一遍,大数据有可能超时。
对暴力搜索进行稍微改进,就演变为思路1,即判断target和A[0]的关系。如果等于A[0],直接返回结果;如果大于A[0],说明需要从0开始搜索,一直到target <= A[i]为止;如果小于A[0],说明需要从len-1处开始搜索,一直到target >= A[i]为止。这样做的原因是有序数组被分成两部分,那么A[0]肯定是大于A[len-1]的。
思路2,即采取二分搜索,初始化是low = 0,high = len - 1,每次计算mid = (low + high) >> 1。如果A[mid] = target,说明找到,直接返回index或者true。否则的话,需要判断A[mid]和A[low]的关系,这样就知道需要怎么搜索了。如果A[mid]大于A[low],说明数组的前半部分是有序的。如果target在前半部分,那么只需要high = mid - 1就可以了,否则low = mid + 1。如果A[mid]小于A[low],说明数组的后半部分是有序的。判断target是否在数组的后半部分。如果在,low = mid + 1,否则high = mid - 1。还有最关键的,如果A[low]和A[mid]相等,那么只有在low+1和high中搜索了。
不管是否有重复数据,其实二分的思路是一致的。但是思路1,代码就需要稍微改改。个人认为,其实思路1好理解一些,在有重复数据的情况下,速度比二分查找还要快一些。

1、Java AC 1

public class Solution {
    public int search(int[] A, int target) {
        if(A == null || A.length == 0){
            return 0;
        }
        int len = A.length;
        if(target > A[0]){
            int i = 1;
            while(i < len && A[i-1] < A[i] && target > A[i]){
                i++;
            }
            if (i >= len) {
				return -1;
			}
            if(target == A[i]){
                return i;
            }else{
                return -1;
            }
        }else if(target < A[0]){
            int i = len - 1;
            while(i > 0 && A[i] > A[i-1] && target < A[i]){
                i--;
            }
            if (i <= 0) {
				return -1;
			}
            if(target == A[i]){
                return i;
            }else{
                return -1;
            }
        }
        return 0;
    }
}
1、Java AC 2

public class Solution {
    public int search(int[] A, int target) {
        if(A == null || A.length == 0){
            return 0;
        }
        int len = A.length;
        int low = 0;
        int high = len-1;
        int mid = -1;
        while(low <= high){
            mid = (low + high) >> 1;
            if(A[mid] == target){
                return mid;
            }
            if(A[mid] > A[low]){
                if(A[mid] > target && A[low] <= target){
                    high = mid - 1;
                }else{
                    low = mid + 1;
                }
            }else if(A[mid] < A[low]){
                if(target > A[mid] && target <= A[high] ){
                    low = mid + 1;
                }else{
                    high = mid - 1;
                }
            }else{
                low ++;
            }
        }
        return -1;
    }
}
2、Java AC 1

public class Solution {
    public boolean search(int[] A, int target) {
        if(A == null || A.length == 0){
            return false;
        }
        int len = A.length;
        if(target > A[0]){
            int i = 1;
            while(i < len && A[i-1] <= A[i] && target > A[i]){
                i++;
            }
            if (i >= len) {
				return false;
			}
            if(target == A[i]){
                return true;
            }else{
                return false;
            }
        }else if(target < A[0]){
            int i = len - 1;
            while(i > 0 && A[i] >= A[i-1] && target < A[i]){
                i--;
            }
            if (i <= 0) {
				return false;
			}
            if(target == A[i]){
                return true;
            }else{
                return false;
            }
        }
        return true;
    }
}
2、Java AC 2

public class Solution {
    public boolean search(int[] A, int target) {
        if(A == null || A.length == 0){
            return false;
        }
        int len = A.length;
        int low = 0;
        int high = len-1;
        int mid = -1;
        while(low <= high){
            mid = (low + high) >> 1;
            if(A[mid] == target){
                return true;
            }
            if(A[mid] > A[low]){
                if(A[mid] > target && A[low] <= target){
                    high = mid - 1;
                }else{
                    low = mid + 1;
                }
            }else if(A[mid] < A[low]){
                if(target > A[mid] && target <= A[high] ){
                    low = mid + 1;
                }else{
                    high = mid - 1;
                }
            }else{
                low ++;
            }
        }
        return false;
    }
}

你可能感兴趣的:(【LeetCode】Search in Rotated Sorted Array && Search in Rotated Sorted Array II)