leetcode 6. 在有序数组旋转后搜索 Search in Rotated Sorted Array

Search in Rotated Sorted Array

难度:Hard



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.

(下一题将研究存在重复的情况Search in Rotated Sorted Array II)



解题思路:

对于数组搜索问题,时间复杂度大致有三种O(n),O(lg n),O(1)

其中线性复杂度 O(n),即顺序搜索;而常数时间O(1)一般通过关键字索引或Hash表实现;而对数复杂度O(lg n),常通过二分查找、二叉搜索树类型实现

对于本题中的条件是有序数组的变形,因此可联想到“快速排序的选择”–二分查找

难点就是寻找划分元素和对划分元素左右两侧的递归条件及其边界

对于划分元素,容易想到直接取中间 m = (l+r) / 2; 数组边界 [l, r)

对实例观察:

0 1 2 4 5 6 7

4 5 6 7 0 1 2

5 1 3

3 1

nums[m] > nums[l] : (l, m-1)单增

nums[m] <= nums[l] : (m+1, r)单增

注:

- 当数组取边界 [l, r) 时,m取到居中靠右(如在2个元素时,m = 1),因此此时,应该比较nums[m], num[l]

- 当取数组边界 [l, r] 时,m取到居中靠左(如在2个元素时,m = 0),因此此时,应该比较nums[m], num[r];

数组边界为 [l,r) – O(lg n)

class Solution {
public:
    //nums 数组边界为 [l,r)
    int searchR(vector<int>& nums,int l, int r, int target) {
        if (r <= l)
            return -1;
        int m = (l+r) / 2;
        if (nums[m] == target)
            return m;

        if (nums[l] < nums[m]) {
            if (target >= nums[l] && target < nums[m])
                return searchR(nums, l, m, target);
            else
                return searchR(nums, m+1, r, target);
        } else {
            if(target > nums[m] && target <= nums[r-1])
                return searchR(nums, m+1, r, target);
            else
                return searchR(nums, l, m, target);
        }
    }

    int search(vector<int>& nums, int target) {
        return searchR(nums, 0, nums.size(), target);
    }
};

数组边界[l, r] –注意传入数组的边界

class Solution {
public:
                    //nums 数组边界为 [l,r]
    int searchR(vector<int>& nums,int l, int r, int target) {
        if (r < l)
            return -1;
        int m = (l+r) / 2;
        if (nums[m] == target)
            return m;

        if (nums[r] > nums[m]) {
            if (target > nums[m] && target <= nums[r])
                return searchR(nums, m+1, r, target);
            else
                return searchR(nums, l, m-1, target);
        } else {
            if(target >= nums[l] && target < nums[m])
                return searchR(nums, l, m-1, target);
            else
                return searchR(nums, m+1, r, target);
        }

    }
    int search(vector<int>& nums, int target) {
        return searchR(nums, 0, nums.size()-1, target);
    }
};

顺序搜索O(n) – 不推荐

class Solution {
public:

    int search(vector<int>& nums, int target) {
        int i = 0;
        for (; i < nums.size(); i++) {
            if (nums[i] == target)
                break;
        }
        if(nums.size() == i)
            return -1;
        else
            return i;
    }
};

(下一题将研究存在重复的情况Search in Rotated Sorted Array II)

时间: 2024-10-29 00:52:04

leetcode 6. 在有序数组旋转后搜索 Search in Rotated Sorted Array的相关文章

leetcode 7. 在有序可重复数组旋转后搜索 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. 解题思路: 本题基于

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 搜索旋转排序数组 python实现

题目描述: python实现 Search in Rotated Sorted Array 搜索旋转排序数组   中文:假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回 -1 . 你可以假设数组中不存在重复的元素. 你的算法时间复杂度必须是 O(log n) 级别. 英文:Suppose an array sorted i

leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索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. 思路:此题在解的时候,才发现Search in Rotated Sorted Array

(每日算法)LeetCode --- Search in Rotated Sorted Array(旋转数组的二分检索)

Search in Rotated Sorted Array I && II Leetcode 对有序数组进行二分查找(下面仅以非递减数组为例): int binarySort(int A[], int lo, int hi, int target) { while(lo <= hi) { int mid = lo + (hi - lo)/2; if(A[mid] == target) return mid; if(A[mid] < target) lo = mid + 1;

[CareerCup] 11.3 Search in Rotated Sorted Array 在旋转有序矩阵中搜索

11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code to find an element in the array. You may assume that the array was originally sorted in increasing order. EXAMPLE Input: find 5 in {15, 16, 19, 20, 2

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: 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. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

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, otherwise return -1. You may assume no du

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