其他分享
首页 > 其他分享> > 0081. Search in Rotated Sorted Array II (M)

0081. Search in Rotated Sorted Array II (M)

作者:互联网

Search in Rotated Sorted Array II (M)

题目

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).

You are given a target value to search. If found in the array return true, otherwise return false.

Example 1:

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

Example 2:

Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false

Follow up:


题意

将一非递减数列的随机后半部分与前半部分换位,得到新数组,在新数组中查找目标值。

思路

33. Search in Rotated Sorted Array 相比,允许数组中存在重复的值,这带来的问题是,很难根据一个元素与最左侧元素的大小关系来判断它是落在左区间还是右区间,如数组 [2, 2, 2, 3, 4, 5, 2, 2]。

因此在 33. Search in Rotated Sorted Array 解法的基础上做出改动:只有当nums[mid]与nums[left]存在严格的大于小于关系时,才能确定mid是落在左区间还是右区间,而当nums[mid]等于nums[left]时,因为无法有效判断区间归属,只能令left右移(或者right左移)。在最坏情况下(全元素一样),时间复杂度退化至\(O(N)\)。


代码实现

Java

class Solution {
    public boolean search(int[] nums, int target) {
        if (nums.length == 0) {
            return false;
        }

        int left = 0, right = nums.length - 1;

        while (left <= right) {
            int mid = (left + right) / 2;
            if (nums[mid] == target) {
                return true;
            }
            // 只有严格的大于小于关系才能明确判断落在左区间还是右区间
            if (nums[mid] > nums[left]) {
                if (target >= nums[left] && target < nums[mid]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            } else if (nums[mid] < nums[left]) {
                if (target <= nums[right] && target > nums[mid]) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            } else {
                left++;
            }
        }

        return false;
    }
}

JavaScript

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {boolean}
 */
var search = function (nums, target) {
  let left = 0, right = nums.length - 1

  while (left <= right) {
    let mid = Math.trunc((right - left) / 2) + left

    if (nums[mid] == target) {
      return true
    }

    if (nums[mid] > nums[left] && target >= nums[left] && target < nums[mid]) {
      right = mid - 1
    } else if (nums[mid] > nums[left]) {
      left = mid + 1
    } else if (nums[mid] < nums[left] && target <= nums[right] && target > nums[mid]) {
      left = mid + 1
    } else if (nums[mid] < nums[left]) {
      right = mid - 1
    } else {
      left++
    }
  }

  return false
}

标签:Search,right,target,nums,Rotated,mid,else,II,left
来源: https://www.cnblogs.com/mapoos/p/14012593.html