leetcode笔记:Search in Rotated Sorted Array

一.题目描写叙述

二.解题技巧

因为这道题出现了旋转的情况,即比第一个元素小的元素可能出如今数值的后半段或者不出现。

因此。能够考虑採用变种的二分查找,即在比較中间元素与目标之前,先比較第一个元素与目标的关系。这个时候,会出现三种情况:

1.第一个元素刚好等于目标,返回第一个元素的坐标,函数结束;

2.第一个元素大于目标。那么目标就可能存在被旋转到数组后面的情况,这个时候,还要比較与数组中间元素的关系,这个时候又会有三种情况:

a.中间元素大于第一个元素,这个时候。目标可能存在于数组的后半段中,递归调用函数,寻找目标的坐标;
b.中间元素等于目标。返回中间元素的坐标,函数结束;
c.中间元素小于第一个元素。这个时候。又能够分为两种情况进行:

    (1).中间元素小于目标元素。那么目标元素可能存在于数组的后半段中,递归调用函数,寻找目标的坐标;
    (2).中间元素大于目标元素。那么目标元素可能存在于数组的前半段中,递归调用函数。寻找目标的坐标;

3.第一个元素小于目标,这是也有三种情况须要考虑:

a.中间元素等于目标元素,返回中间元素的坐标,函数结束;
b.中间元素大于第一个元素,这个时候,也有两种情况要考虑:

    (1).中间元素大于目标,那么目标元素可能存在于数组的前半段中,递归调用函数,寻找目标的坐标;
    (2).中间元素小于目标,那么目标元素可能存在于数组的后半段中,递归调用函数,寻找目标的坐标;

c.中间元素小于第一个元素,那么目标元素可能存在于数组的前半段中,递归调用函数,寻找目标的坐标;

当然,还须要考虑数组的元素个数为0,1, 2,的情况,以及对于递归的过程中数组的起始位置坐标以及数组中元素的个数。这些才是这道题的难点所在,我也是调试了非常久才调通代码的。

三.演示样例代码

// 时间复杂度O(log n)。空间复杂度O(1)
#include <iostream>

using namespace std;

class Solution
{
public:
    int SearchRotatedSortedArray(int A[], int n, int target)
    {
        int start = 0;
        int end = n;
        int middle = start + (end - start) / 2;
        while (start != end)
        {
            if (target == A[middle])
                return middle;
            if (A[start] < A[middle])
            {
                if ((target < A[middle]) && (A[start] <= target))
                    end = middle;
                else
                    start = middle + 1;
            }
            else
            {
                if ((target > A[middle]) && (target <= A[end - 1]))
                    start = middle + 1;
                else
                    end = middle;
            }
        }
        return -1; // 在数组中找不到目标元素时返回-1
    }
};

四.体会

这答题的难点在于边界条件和递归过程中的数组的第一个元素的指针设置和数组元素个数的设置上面,边界条件常常是面试题考查的重点。

时间: 2024-10-10 00:12:54

leetcode笔记: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

【LeetCode】Search in Rotated Sorted Array II (2 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. 解法一:顺序查找 cl