打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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:

  • This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
  • Would this affect the run-time complexity? How and why?

题意

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

思路

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
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
153 Find Minimum in Rotated Sorted Array JAVA实现
0977. Squares of a Sorted Array (E)
面试随缘刷题--day3
LeetCode 81. 搜索旋转排序数组 II
203,查找-插值查找
聊一聊二分查找法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服