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;
    }
};
时间: 2024-10-13 23:11:40

Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII的相关文章

Why is processing a sorted array faster than an unsorted array(Stackoverflow)

What is Branch Prediction?Consider a railroad junction:Image by Mecanismo, via Wikimedia Commons. Used under the CC-By-SA 3.0 license.Now for the sake of argument, suppose this is back in the 1800s - before long distance or radio communication.You ar

[ES2016] Check if an array contains an item using Array.prototype.includes

We often want to check if an array includes a specific item. It's been common to do this with the Array.prototype.indexOf method, but now we have a simpler way: We can use the Array.prototype.includes method, which is available starting with ES2016.

JavaScript,通过分析Array.prototype.push重新认识Array

在阅读ECMAScript的文档的时候,有注意到它说,数组的push方法其实不仅限于在数组中使用,专门留作通用方法.难道是说,在一些类数组的地方也可以使用?而哪些是和数组非常相像的呢,大家或许一下子就可以想到就是Object对象.因为Array就是继承自Object的,可以用 [] instanceof Object,会发现返回的是true.当然大家都知道,这也不是什么新鲜事.那我们可以大胆尝试一下,如果我们将数组的push方法应用在对象上,会一个怎么样的表现呢? 我们通过call,将this的

numpy array转置与两个array合并

我们知道,用 .T 或者 .transpose() 都可以将一个矩阵进行转置. 但是一维数组转置的时候有个坑,光transpose没有用,需要指定shape参数, 在array中,当维数>=2,时这个成立,但=1时,就不成立了,如: In [7]: yOut[7]: array([0, 0, 0, 0, 0]) In [14]: y.TOut[14]: array([0, 0, 0, 0, 0]) In [15]: y.transpose()Out[15]: array([0, 0, 0, 0,

LeetCode 33 Search in Rotated Sorted Array(在旋转排序数组中搜索)(*)

翻译 假定一个数组在一个我们预先不知道的轴点旋转. 例如,0 1 2 4 5 6 7可能会变为4 5 6 7 0 1 2. 给你一个目标值去搜索,如果找到了则返回它的索引,否则返回-1. 你可以假定没有重复的元素存在于数组中. 原文 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

LintCode: 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 duplic

LeetCode: Search in Rotated Sorted Array 解题报告

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 retu

LeetCode: Search in Rotated Sorted Array II 解题报告

Search in Rotated Sorted Array II Follow up for "LeetCode: 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 arr

33. Search in Rotated Sorted Array &amp;&amp; 81. Search in Rotated Sorted Array II &amp;&amp;

33. 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

[leedcode 33] 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 duplic