[Leetcode] Search in Rotated Sorted Array 系列

Search in Rotated Sorted Array 系列题解

题目来源:

Search in Rotated Sorted Array

Search in Rotated Sorted Array II


第一版

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.

Solution

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

        if (low > 0 && target >= nums[0]) {
            high = low - 1;
            low = 0;
        } else {
            high = size - 1;
        }
        while (low <= high) {
            mid = (low + high) / 2;
            if (target == nums[mid])
                return mid;
            else if (target > nums[mid])
                low = mid + 1;
            else
                high = mid - 1;
        }
        return -1;
    }
};

解题描述

这道题题意是,给出一个数组,数组原本是升序的,但是被从中间某个位置截断,后面一段被放到前面,要求在数组中找到目标数字target的位置。这里首选的做法肯定是二分查找,不过要先用二分查找找到截断的位置,然后根据target与数组第一个元素的大小关系决定其所在半区,再进行二分查找。


进阶版本

Follow up for "Search in Rotated Sorted Array":

What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

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).

Write a function to determine if a given target is in the array.

The array may contain duplicates.

Solution

解法一:从左向右遍历数组,找到数组旋转的位置

class Solution {
public:
    bool search(vector<int>& nums, int target) {
        if (nums.empty()) return false;
        int size = nums.size();
        int low = 0, high = size - 1, mid;
        for (; low < size; low++) {
            if (low > 0 && nums[low - 1] > nums[low]) break;
        }
                
        if (low > 0 && target >= nums[0]) {
            high = low - 1;
            low = 0;
        } else {
            high = size - 1;
        }
        while (low <= high) {
            mid = (low + high) / 2;
            if (target == nums[mid])
                return true;
            else if (target > nums[mid])
                low = mid + 1;
            else
                high = mid - 1;
        }
        return false;
    }
};

解法二:使用二分查找,结合查重夹逼

class Solution {
public:
    bool search(vector<int>& nums, int target) {
        if (nums.empty()) return false;
        int low = 0, high = nums.size() - 1, mid;
        while (low <= high) {
            mid = (low + high) / 2;
            if (nums[mid] == target) return true;

            if (nums[low] == nums[mid] && nums[mid] == nums[high]) {
                ++low;
                --high;
            } else if (nums[low] <= nums[mid]) {
                if (nums[low] <= target && nums[mid] > target)
                    high = mid - 1;
                else
                    low = mid + 1;
            } else {
                if (nums[mid] < target && nums[high] >= target)
                    low = mid + 1;
                else
                    high = mid - 1;
            }
        }
        return false;
    }
};

解题描述

这道题题意是,给出一个排好序的数组,在数组中间某个位置截断,将后面一段放到前面,并且数组中可能存在重复的元素,然后要求判断数组中是否存在目标数字target。上面给出了2种解法,第一种解法相对来说比较暴力,平均的时间复杂度为O(n / 2)。而第二种解法虽然在出现高低位游标元素重复的时候进行了线性移位,但是最优时间复杂度位O(log n),最差时间复杂度是O(n),相对来说还是有一定的优化。

posted @ 2018-02-22 11:57  言何午  阅读(113)  评论(0编辑  收藏  举报