Search in Rotated Sorted Array && Search in Rotated Sorted ArrayII

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.

解题思路:

求旋转数组中是否存在某一个整数,若存在返回该值在数组中的下标,否则返回-1.该数组中无重复数字。

方法:

参考Find Minimum in Rotated Sorted Array I  http://blog.csdn.net/sinat_24520925/article/details/46438537

我们找到旋转数组中的最小的元素,也就相当于将旋转数组,分成两个递增数组的分界点知道了。我们在这两个有序数组中利用二分法找是否存在target即可,代码如下:

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

        }
        else
        {
            int low=0,high=dex-1;
        while(low<=high)
        {
            int mid=(low+high)/2;
            if(nums[mid]>target)
            high=mid-1;
            else if(nums[mid]<target)
            low=mid+1;
            else
            return mid;
        }
         low=dex,high=nums.size()-1;
        while(low<=high)
        {
            int mid=(low+high)/2;
            if(nums[mid]>target)
            high=mid-1;
            else if(nums[mid]<target)
            low=mid+1;
            else
            return mid;
        }
        }
        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.

解题思路:

求旋转数组中是否存在某一个整数,若存在返回true,否则false.该数组中可以有重复数字。

方法:

参考

Find Minimum in Rotated Sorted Array II http://blog.csdn.net/sinat_24520925/article/details/46438537

同Search in Rotated Sorted Array I,相同思路,代码如下:

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

        }
        else
        {
            int low=0,high=dex-1;
        while(low<=high)
        {
            int mid=(low+high)/2;
            if(nums[mid]>target)
            high=mid-1;
            else if(nums[mid]<target)
            low=mid+1;
            else
            return true;
        }
         low=dex,high=nums.size()-1;
        while(low<=high)
        {
            int mid=(low+high)/2;
            if(nums[mid]>target)
            high=mid-1;
            else if(nums[mid]<target)
            low=mid+1;
            else
            return true;
        }
        }
        return false;
    }
};
时间: 2024-10-29 19:12:15

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

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 < hi

Leetcode | Search in Rotated Sorted Array I &amp; II

Search in Rotated Sorted Array I 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 re

39.1: Convert Sorted Array to Binary Search Tree

/************************************************************************/            /*       39.1:  Convert Sorted Array to Binary Search Tree                               */            /************************************************************

Convert Sorted Array to Binary Search Tree &amp; Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 方法:将有序数组转为平衡二叉树,我们知道平衡二叉树的中序遍历是有序数组,而且“中间”的元素可以作为根节点,那么构建树的时候,找出中间元素,构造根元素,然后继续重复上面的方法,构造左子树和右子树.代码如下: 1 /**

[lintcode easy]Recover rotated sorted array

Recover Rotated Sorted Array Given a rotated sorted array, recover it to sorted array in-place. Example [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5] Challenge In-place, O(1) extra space and O(n) time. Clarification What is rotated array? For example, the orgin

Recover Rotated Sorted Array

Given a rotated sorted array, recover it to sorted array in-place. Have you met this question in a real interview? Yes Which company asked you this question? Airbnb Alibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay Facebook Google Hulu Intel Li

Lintcode Recover Rotated Sorted Array

Given a rotated sorted array, recover it to sorted array in-place. Example [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5] Challenge In-place, O(1) extra space and O(n) time. Clarification What is rotated array? For example, the orginal array is [1,2,3,4], The ro

[Leetcode][JAVA] Convert Sorted Array to Binary Search Tree &amp;&amp; Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 很简单的二分法,只要给出Array的开始和结束下标作为参数传入即可. 1 public TreeNode sortedArrayToBST(int[] num) { 2 return constructBST(num,

LeetCode: Convert Sorted Array to Binary Search Tree 解题报告

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST.SOLUTION 1:使用递归解决. base case: 当索引值超过时,返回null. 递归主体:构造根节点,调用递归构造左右子树.并将左右子树挂在根节点上. 返回值:返回构造的根节点. GITHUB: https: