LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

给出排序好的一维无重复元素的数组,随机取一个位置断开,把前半部分接到后半部分后面,得到一个新数组,在新数组中查找给定数的下标,如果没有,返回-1。时间复杂度限制\(O(log_2n)\)

C++

我的想法是先找到数组中最大值的位置。然后以此位置将数组一分为二,然后在左右两部分分别寻找target
二分寻找最大值的时候,因为左半部分的数一定大于nums[l],所以nums[mid]nums[l]比较大小就可以知道最大值的位置在mid的左侧还是右侧。
在两个单调区间分别二分查找target时,可以直接使用STL提供的lower_bound函数

class Solution {
public:
    int search(std::vector<int>& nums, int target) {
        if(nums.empty()) return -1;
        int l = 0,r = nums.size();
        while(l!=r){
            int mid = (l+r)/2;
            if(nums[mid]>nums[l]) l = mid;
            else r = mid;
        }
        auto iter = std::lower_bound(nums.begin(),nums.begin()+l+1,target);
        if(*iter == target && std::distance(nums.begin(),iter)<=l) return std::distance(nums.begin(),iter);
        iter = std::lower_bound(nums.begin()+l+1,nums.end(),target);
        if(*iter == target && std::distance(nums.begin(),iter)<nums.size()) return std::distance(nums.begin(),iter);
        return -1;
    }
};

然鹅标程只用了一次二分就搞定了...
学习改进后:

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

Java

Python3

原文地址:https://www.cnblogs.com/NeilThang/p/10308288.html

时间: 2024-12-24 15:29:21

LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>的相关文章

LeetCode -- Search in Rotated Sorted Array(Binary Search)

题目地址:https://leetcode.com/problems/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 ar

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

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

[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] Search in Rotated Sorted Array I (33) &amp;&amp; II (81) 解题思路

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

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

LeetCode: 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, othe

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