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. PAT甲级1007题解——贪心

    题目分析:对于每一个点来说,如果选择合并入包含前一个点的序列那么只有在前一个点的序列不为负数(这里指的是包含前一个位置的数的一个连续序列的和不为负数),当前点才会将自己也加入这个子序列,否则,当前点则 ...

  2. 后台返回的Json为null的字段不显示的方法

    如果引入的是谷歌的gson的话,需要引入依赖: <dependency> <groupId>com.fasterxml.jackson.core</groupId> ...

  3. 鸡尾酒排序Cocktail Sort (双向冒泡排序)

    鸡尾酒排序 鸡尾酒排序思路,先从左边开始进行冒泡排序,第一趟冒泡排序完,最大值在的数组的最右端,然后进行第二趟排序,第二趟排序从右边开始排序,第二趟结束时,最小值在数组最左端,以此类推,每一趟排序完都 ...

  4. Vue移动端项目如何使用手机预览调试

  5. Git学习笔记--配置(二)

    由之前文章,总结得出Git的特点: 最优的存储能力: 非凡性能: 开源的: 管理成本低: 很容易做备份: 支持离线操作: 很容易定制工作流程: Git is a free and open sourc ...

  6. janusgraph单机版安装

    注:本次安装janusgraph基于es和hbse,所以先安装es和hbase 1.安装jdk 2.安装janusgraph 解压安装文件至/usr/janusgraph-0.3.1 unzip ja ...

  7. Markdown 设置字体大小颜色及背景色

    一.更改字体.大小.颜色 <font face="黑体">我是黑体字</font><font face="微软雅黑">我是微 ...

  8. NOIP爆炸记

    NOIP爆炸游记 Day 0 Day 1 T1 T2 T3 Day 2 T1 T2 T3 最后 Day 0 复习模板 + 做真题 + 方 Day 1 早上吃了一片面包,就进了考场- T1 Exm??这 ...

  9. LeetCode 930. Binary Subarrays With Sum

    原题链接在这里:https://leetcode.com/problems/binary-subarrays-with-sum/ 题目: In an array A of 0s and 1s, how ...

  10. Web API design

    Web API design 28 minutes to read Most modern web applications expose APIs that clients can use to i ...