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.

33. Search in Rotated Sorted Array 的拓展,数组中允许出现重复数字,这个也会影响我们选择哪半边继续搜索,之前判断左右部分是否有序的方法就失效了,因为可能有这种58555情况,虽然起点小于等于中间,但不代表右边就不是有序的,因为中点也小于等于终点,所有右边也是有序的。所以,如果遇到这种中点和两边相同的情况,我们两边都要搜索。

Java:

public class Solution {
public boolean search(int[] nums, int target) {
return helper(nums, 0, nums.length - 1, target);
} public boolean helper(int[] nums, int min, int max, int target){
int mid = min + (max - min) / 2;
// 不符合min <= max则返回假
if(min > max){
return false;
}
if(nums[mid] == target){
return true;
}
boolean left = false, right = false;
// 如果左边是有序的
if(nums[min] <= nums[mid]){
// 看目标是否在左边有序数列中
if(nums[min] <= target && target < nums[mid]){
left = helper(nums, min, mid - 1, target);
} else {
left = helper(nums, mid + 1, max, target);
}
}
// 如果右边也是有序的
if(nums[mid] <= nums[max]){
// 看目标是否在右边有序数列中
if(nums[mid] < target && target <= nums[max]){
right = helper(nums, mid + 1, max, target);
} else {
right = helper(nums, min, mid - 1, target);
}
}
// 左右两边有一个包含目标就返回真
return left || right;
}
}

Python:

class Solution(object):
def search(self, nums, target):
left, right = 0, len(nums) - 1 while left <= right:
mid = left + (right - left) / 2 if nums[mid] == target:
return True
elif nums[mid] == nums[left]:
left += 1
elif (nums[mid] > nums[left] and nums[left] <= target < nums[mid]) or \
(nums[mid] < nums[left] and not (nums[mid] < target <= nums[right])):
right = mid - 1
else:
left = mid + 1 return False

  

类似题目:

[LeetCode] 33. Search in Rotated Sorted Array 在旋转有序数组中搜索

All LeetCode Questions List 题目汇总

  

[LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II的更多相关文章

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

  2. LeetCode 153. Find Minimum in Rotated Sorted Array (在旋转有序数组中找到最小值)

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

  3. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  4. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

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

  5. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  6. [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)

    This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...

  7. LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  8. leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  9. LeetCode 81.Search in Rotated Sorted Array II(M)

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

随机推荐

  1. CentOS7怎样安装Redis4.0.12

    一.安装 yum -y install tcl安装相关依赖 mkdir /usr/local/redis创建redis安装目录 cd /usr/local/redis 进入redis目录 wget h ...

  2. php中函数的类型提示和文件读取功能

    这个没有深入. <?php function addNumbers(int $a, int $b, bool $printSum): int { $sum = $a + $b; if ($pri ...

  3. 【后缀表达式求解】No.3.栈-evaluate-reverse-polish-notation题解(Java版)

    牛客网的题目链接 题目描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid opera ...

  4. NOIP2019 PJ 对称二叉树

    题目描述 一棵有点权的有根树如果满足以下条件,则被轩轩称为对称二叉树: 二叉树: 将这棵树所有节点的左右子树交换,新树和原树对应位置的结构相同且点权相等. 下图中节点内的数字为权值,节点外的 id 表 ...

  5. Linux UART驱动分析

    1. 介绍 8250是IBM PC及兼容机使用的一种串口芯片; 16550是一种带先进先出(FIFO)功能的8250系列串口芯片; 16550A则是16550的升级版本, 修复了FIFO相关BUG, ...

  6. 深度学习Keras框架笔记之TimeDistributedDense类

    深度学习Keras框架笔记之TimeDistributedDense类使用方法笔记 例: keras.layers.core.TimeDistributedDense(output_dim,init= ...

  7. BZOJ 4477: [Jsoi2015]字符串树 可持久化trie树

    这个是真——可持久化字典树..... code: #include <bits/stdc++.h> #define N 100006 #define setIO(s) freopen(s& ...

  8. 开源项目 01 HtmlAgilityPack

    using HtmlAgilityPack; using System; using System.Collections.Generic; using System.Linq; using Syst ...

  9. Call to undefined function imagecreatefromjpeg() 让GD支持JPEG格式的图片扩展

    安装扩展支持jpeg格式: 第一步:首先下载文件: 版本v8: wget http://www.ijg.org/files/jpegsrc.v8b.tar.gz 版本v9: wget http://w ...

  10. Mysql 随机查询10条数据效率最快的查询方法

    1)使用join 和 rand() 耗时 0.009 SELECT * FROM `t_topic` AS t1 JOIN ( SELECT ROUND( RAND() * ( (SELECT MAX ...