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 duplicate exists in the array.

夹着一个有序数组在未知的某点上旋转过了。

(例如 0 1 2 3 4 5 6 7 可能变成 4 5 6 7 0 1 2)

在数组中查找一个特定值。如果找到返回下标,否则返回-1。

假设不存在数组里没有重复的元素。

对于有序数组可以采用二分法,对于旋转过的有序数组也可以使用二分法。只不过这时候数组会分割成两段,且前一段大于后一段。令A为数组,要查找的值为T,I为起始点,J为终点,M为中点,当M在前半段,即A[M]≥A[i]时,那么如果A[i] ≤t≤A[M]则继续在前半段查找,否则在后半段查找;当M在后半段,即A[M]<A[i],那么如果A[M]≤t≤A[J]则继续在后半段查找,否则在前半段查找。时间复杂度是O(logN),N为数组的长度。给出代码如下:

int search(vector<int>& nums, int target) {
    int i = 0;
    int j = nums.size()-1;
    while (i<=j)
    {
        int mid = i + (j-i)/2;
        int nmid = nums[mid];
        if (target == nmid)
            return mid;

        if (nums[i] <= nums[j])
        {
            if (target < nmid)
                j = mid-1;
            else i = mid+1;
        }
        else
        {
            if (nmid >= nums[i])
            {
                if (target >= nums[i] && target < nmid)
                    j = mid-1;
                else i = mid+1;
            }
            else
            {
                if (target > nmid && target <= nums[j])
                    i = mid+1;
                else j = mid-1;
            }
        }
    }

    return -1;
}

对于81. Search in Rotated Sorted Array II,允许数组中有重复值,只要在这个算法做微小的改动,使得右半段小于左半段即可,右半段可能等于左半段的只有最后一段,所以只要删除那些等于开头的末尾即可,注意要至少保留一个元素。

时间: 2024-10-27 04:33:00

33. Search in Rotated Sorted Array的相关文章

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 [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][Python]33: Search in Rotated Sorted Array

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 33: Search in Rotated Sorted Arrayhttps://oj.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

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题意分析&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 return -1. You may assume no duplic

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

33. Search in Rotated Sorted Array (Array;Divide-and-Conquer)

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