【一天一道LeetCode】#33. Search in Rotated Sorted Array

一天一道LeetCode

本系列文章已全部上传至我的github,地址:

https://github.com/Zeecoders/LeetCode

欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

一个排好序的数组,经过一定的旋转之后得到新的数组,在这个新的数组中查找一个目标数。

那么,首先我们需要在旋转后的数组中找到原数组的起始点,并将数组分成两部分。

例如:4,5,6,7,0,1,2分为4,5,6,7和0,1,2

然后,确定目标数在哪一个部分,

最后,采用二分法来进行加速搜索!

具体看代码:

未优化版本

class Solution {
public:

    int search(vector<int>& nums, int target) {

        int len = nums.size();

        int i = 0 ; 

        int j = len-1;

        int p = 0 ;

        while(p+1<len&&nums[p]<nums[p+1]) p++;//找出第一个破坏升序的数即为旋转中心

        if(nums[0]<=target) j=p;//确定被查找的数在哪个部分

        else if(nums[len-1]>=target) i=p+1;

        while(i<=j)//二分搜索

        {

            int mid = (i+j)/2;

            if(nums[mid] == target) return mid;

            else if(nums[mid] >target) j= mid-1;

            else i = mid+1;

        }

        return -1;//未找到则返回-1

    }

};

AC之后一看运行时间8ms,看来代码还有待优化,于是在查找旋转中心的方法上进行优化,采用二分法进行搜索旋转中心。

优化版本


class Solution {

public:

    int search(vector<int>& nums, int target) {

        int len = nums.size();

        int i = 0;

        int j = len - 1;

        int p = 0;

        bool isfind = false;

        if (nums[0]>nums[len-1])//如果进行了旋转

        {

            while (i < j)//二分搜索旋转中心

            {

                if (i == j - 1) {

                    isfind = true;

                    break;

                }

                int mid = (i + j) / 2;

                if (nums[i] < nums[mid]) i = mid;

                if (nums[j] > nums[mid]) j = mid;

            }

        }

        if (isfind)//找到了旋转中心

        {

            if (nums[0] <= target) { j = i; i = 0; }

            if (nums[len - 1] >= target) { i = j; j = len - 1;}

        }

        while (i<=j)二分搜索目标数

        {

            int mid = (i + j) / 2;

            if (nums[mid] == target) return mid;

            else if (nums[mid] >target) j = mid-1;

            else i = mid+1;

        }

        return -1;

    }

};

优化后的版本运行时间4ms,快了一半!

时间: 2024-09-30 21:09:02

【一天一道LeetCode】#33. Search in Rotated Sorted Array的相关文章

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

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

LeetCode 33 Search in Rotated Sorted Array (C,C++,Java,Python)

Problem: 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

19.2.2 [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.

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

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