Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII

一:Search in Sorted Array

二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环。

代码:

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int numsSize = nums.size();
        int low = 0,high = numsSize;
        while(low < high){
            int mid = low + (high-low)/2;
            if(nums[mid]==target){
                return mid;
            }else if(nums[mid]<target){
                low = mid+1;
            }else if(nums[mid]>target){
                high = mid;
            }
        }
        return -1;
    }
};

二: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.

数组中不存在重复元素

这时没法像通常的二分查找那样的直接定位target在中点元素的哪一端,需要进行讨论。如果target<A[mid],有两种可能,一种是mid在左边有序数组,另一种可能是mid在右侧的有序数组。mid在左边有序数组,那么target又有两种可能,可以在左边有序数组,也可在右边有序数组;如果mid在右侧有序数组,那么只有一种可能,只能在右侧数组。同理可以讨论target>A[mid]时的情况。而mid在左侧有序数组还是右侧有序数组可以通过A[mid]>A[low]?的关系确定。当然可以画图分析,红线部分表示mid可能的位置:

代码:

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int numsSize = nums.size();
        int low = 0,high = numsSize;
        while(low < high){
            int mid = low + (high-low)/2;
            if(nums[mid]==target){
                return mid;
            }else if(nums[mid]<target){
                if(nums[mid]>nums[low]){//mid位于左边区域
                    low = mid+1;
                }else{//mid位于右边区域
                    if(target>nums[low]){
                        high = mid;
                    }else if(target == nums[low]){
                        return low;
                    }else{
                        low = mid+1;
                    }
                }
            }else if(nums[mid]>target){
                if(nums[mid]>nums[low]){//mid位于左边区域,target有两个可能的区域位置
                    if(target>nums[low]){
                        high = mid;
                    }else if(target == nums[low]){
                        return low;
                    }else{
                        low = mid+1;
                    }
                }else{//mid位于右边区域
                    high = mid;
                }
            }
        }
        return -1;
    }
};

三:Search in Rotated Sorted ArrayII

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)。

如果有重复元素,当A[mid]>=A[low]时,我们无法确定mid在左边有序数组还是右边有序数组,可以画图理解,如下两幅图,红线部分表示mid的位置,两幅图中均有A[mid]>=A[low]

  

由于当target>A[mid]时,我们无法确定mid在哪个有序数组中,所以我们没法讨论了,此时,我们将low上升一个,high下降一个。

代码:

class Solution {
public:
    bool search(vector<int>& nums, int target) {
        int numsSize = nums.size();
        int low = 0,high = numsSize;
        while(low < high){
            int mid = low + (high-low)/2;
            if(nums[mid]==target){
                return true;
            }else if(nums[mid]<target){
                if(nums[mid]>=nums[low]){//无法确定mid在左边区域还是在右边区域
                    if(target == nums[low] || target==nums[high-1]){
                        return true;
                    }
                    low++;
                    high--;
                }else{//mid位于右边区域
                    if(target>nums[low]){
                        high = mid;
                    }else if(target == nums[low]){
                        return true;
                    }else{
                        low = mid+1;
                    }
                }
            }else if(nums[mid]>target){
                if(nums[mid]>=nums[low]){//无法确定mid在左边区域还是在右边区域
                    if(target == nums[low] || target==nums[high-1]){
                        return true;
                    }
                    low++;
                    high--;
                }else{//mid位于右边区域
                    high = mid;
                }
            }
        }
        return false;
    }
};

 

posted @ 2015-11-12 12:21  zengzy  阅读(277)  评论(0编辑  收藏  举报
levels of contents