leetcode No33. Search in Rotated Sorted Array

Question:

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.

Algorithm:

先找顺序,再找target

有三种情况:(至少有一半是顺序的)

1、nums[mid]==target

2、nums[first]<nums[mid](左半是顺序,如果target在范围内,二分,如果不在,则在右半找顺序,即重复1,2,3)

3、nums[first]>=nums[mid](右半是顺序,如果target在范围内,二分,如果不在,则在左半找顺序,则重复1,2,3)

Accepted Code:

/*
     旋转数组只能有三种情况:
     1. 左半部分和有半部分都是按顺序的。旋转主元恰好是中间的数字。
     2. 只有左半部分是按顺序的。旋转主元在左半部分。
     3. 只有右半部分是按顺序的。旋转主元在右半部分。
     所以至少有一半是按顺序的,可以用排除法确定每一次二分查找的
     上界和下界。
     1. 左半部分是按顺序
     2. 左半部分不是按顺序的,那么右半部分肯定是按顺序的
     不断缩小范围。
*/
class Solution {
public:
    int search(vector<int>& nums, int target) {
        int res=-1;
        int l=0;
        int r=nums.size()-1;
        int mid=0;
        while(l<=r)
        {
            int mid=l+(r-l)/2;
            if(nums[mid]==target)
                return mid;
            else if(nums[l]<nums[mid])          //mid左边是顺序
            {
                if(nums[l]<=target&&nums[mid-1]>=target)
                    r=mid-1;
                else
                    l=mid+1;
            }
            else                                   //mid右边是顺序
            {
                if(nums[mid+1]<=target&&nums[r]>=target)
                    l=mid+1;
                else
                    r=mid-1;
            }
        }
        if(nums[mid]==target)
            return mid;
        else
            return -1;
    }
};
时间: 2024-08-01 10:44:28

leetcode No33. Search in Rotated Sorted Array的相关文章

Java for LeetCode 081 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. 解题思路: 参考Java for LeetCode 033 Search in Rota

LeetCode Solutions : 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. Java Solution ( refer to my blog LeetCode So

[LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. Search in Rotated Sorted Array (Hard) 链接: 题目:https://leetcode.com/problems/search-in-rotated-sorted-array/ 代码(github):https://github.com/illuz/leetcode 题

LeetCode 33 Search in Rotated Sorted Array [binary search] &lt;c++&gt;

LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前半部分接到后半部分后面,得到一个新数组,在新数组中查找给定数的下标,如果没有,返回-1.时间复杂度限制\(O(log_2n)\) C++ 我的想法是先找到数组中最大值的位置.然后以此位置将数组一分为二,然后在左右两部分分别寻找target. 二分寻找最大值的时候,因为左半部分的数一定大于nums[l],所以n

[Lintcode]62. Search in Rotated Sorted Array/[Leetcode]33. Search in Rotated Sorted Array

[Lintcode]62. Search in Rotated Sorted Array/[Leetcode]33. Search in Rotated Sorted Array 本题难度: Medium/Medium Topic: Binary Search Description Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (

LeetCode 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)

Suppose an array sorted in ascending order 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

【leetcode】Search in Rotated Sorted Array II

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. 与I类似,只是在二分搜

【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 No81. Search in Rotated Sorted Array II

Question: 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. 在旋转数组中找target,与I不同的是,这次存在重复元素 Algo