当前位置 : 首页 » 文章分类 :  算法  »  LeetCode.033.Search in Rotated Sorted Array 搜索旋转有序数组

LeetCode.033.Search in Rotated Sorted Array 搜索旋转有序数组

题目描述

033 Search in Rotated Sorted Array
https://leetcode-cn.com/problems/search-in-rotated-sorted-array/
https://leetcode.com/problems/search-in-rotated-sorted-array/description/

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 may assume no duplicate exists in the array.
Your algorithm’s runtime complexity must be in the order of O(log n).

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

相似题目

LeetCode.033.Search in Rotated Sorted Array 搜索旋转有序数组
LeetCode.153.Find Minimum in Rotated Sorted Array 旋转有序数组中的最小值


解题过程

这道题是153题寻找旋转有序数组的最小值的扩展,在旋转有序数组中查找指定值。还是low,high双指针二分搜索。
每次判断出单边有序后,就紧接着判断目标值是否在单边有序子数组中,从而可以一下子砍掉一半,实现O(logn)搜索
时间复杂度 O(logn),空间复杂度 O(1)

不过还是需要注意像{3,1}, {3,1,2}, {2,3,1}这样的容易出错的用例

SolutionV2018

private static class SolutionV2018 {
    public int search(int[] nums, int target) {
        int low = 0;
        int high = nums.length - 1;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[mid] >= nums[low]) {//左边有序,转折点在右边
                if (target >= nums[low] && target < nums[mid]) {//target在左边
                    high = mid - 1;
                } else {
                    low = mid + 1;
                }
            } else {//右边有序,转折点在左边
                if (target > nums[mid] && target <= nums[high]) {//target在右边
                    low = mid + 1;
                } else {
                    high = mid - 1;
                }
            }
        }
        return -1;
    }
}

SolutionV2020

private static class SolutionV2020 {
    public int search(int[] nums, int target) {
        if (null == nums || nums.length == 0) {
            return -1;
        }
        int low = 0, high = nums.length -1;

        while (low <= high) {
            int mid = (low + high) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            if (mid - 1 >= low && nums[low] <= nums[mid - 1]) {
                // 左边是有序数组
                if (target >= nums[low] && target <= nums[mid - 1]) {
                    high = mid -1;
                } else {
                    low = mid +1;
                }
            } else {
                // 右边是有序数组
                if (mid + 1 <= high && target >= nums[mid + 1] && target <= nums[high]) {
                    low = mid + 1;
                } else {
                    high = mid - 1;
                }
            }
        }
        return -1;
    }
}

SolutionV2020V2

2020年第二遍写这个题:
1、mid值计算的写法改进了,int mid = left + (right - left) / 2; 避免了一个二分搜索的经典溢出bug
2、写法好像简化了一些

private static class SolutionV2020V2 {
    public int search(int[] nums, int target) {
        if (null == nums || nums.length == 0) {
            return -1;
        }
        int left = 0, right = nums.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (target == nums[mid]) {
                return mid;
            }
            if (right - left == 1) {
                return target == nums[left] ? left : (target == nums[right] ? right : -1);
            }
            if (nums[left] < nums[mid] ) {
                // 左边是升序,拐点在右边
                if (target >= nums[left] && target < nums[mid]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            } else {
                // 右边是升序,拐点在左边
                if (target > nums[mid] && target <= nums[right]) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
        }
        return -1;
    }
}

找到最小值位置后转化为常规二分搜索

讨论区看到一个我比较喜欢的方法,先找到最小值的下标位置,然后确定目标值在哪个有序子数组中,从而转化为常规二分搜素:

public int search(int[] nums, int target) {
    int minIdx = findMinIdx(nums);
    if (target == nums[minIdx]) return minIdx;
    int m = nums.length;
    int start = (target <= nums[m - 1]) ? minIdx : 0;
    int end = (target > nums[m - 1]) ? minIdx : m - 1;

    while (start <= end) {
        int mid = start + (end - start) / 2;
        if (nums[mid] == target) return mid;
        else if (target > nums[mid]) start = mid + 1;
        else end = mid - 1;
    }
    return -1;
}

public int findMinIdx(int[] nums) {
    int start = 0, end = nums.length - 1;
    while (start < end) {
        int mid = start + (end -  start) / 2;
        if (nums[mid] > nums[end]) start = mid + 1;
        else end = mid;
    }
    return start;
}

GitHub代码

algorithms/leetcode/leetcode/_033_SearchInRotatedSortedArray.java
https://github.com/masikkk/algorithms/blob/master/leetcode/leetcode/_033_SearchInRotatedSortedArray.java


上一篇 MySQL-基础

下一篇 LeetCode.153.Find Minimum in Rotated Sorted Array 旋转排序数组中的最小值

阅读
评论
971
阅读预计5分钟
创建日期 2018-03-02
修改日期 2020-04-27
类别

页面信息

location:
protocol:
host:
hostname:
origin:
pathname:
href:
document:
referrer:
navigator:
platform:
userAgent:

评论