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

思路

该题是[leetcode]33. Search in Rotated Sorted Array旋转过有序数组里找目标值 的followup

唯一区别是加了line24-26的else语句来skip duplicates

代码

 class Solution {
public boolean search(int[] nums, int target) {
// corner case
if (nums.length == 0) return false;
// init
int left = 0;
int right = nums.length - 1; while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) return true;
else if (nums[mid] < nums[right]) { // right side is sorted
if (target > nums[mid] && target <= nums[right]) {
left = mid + 1;
} else {
right = mid - 1;
}
} else if(nums[mid] > nums[right]){ // left side is sorted
if (target < nums[mid] && target >= nums[left]) {
right = mid - 1;
} else {
left = mid + 1;
}
}else{
right--; //skip duplicats
}
}
return false;
}
}

[leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)的更多相关文章

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

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

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

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

  3. [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. ...

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

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

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

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

  6. [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. ...

  7. LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  8. leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  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. Go语言开发Windows应用

    Go语言开发Windows应用 当第一次看到Go程序在windows平台生成可执行的exe文件,就宣告了windows应用也一定是Go语言的战场.Go不是脚本语言,但却有着脚本语言的轻便简单的特性.相 ...

  2. python找递归目录中文件,并移动到一个单独文件夹中,同时记录原始文件路径信息

    运营那边有个需求. 下载了一批视频文件,由于当时下载的时候陆陆续续创建了很多文件夹,并且,每个文件夹下面还有子文件夹以及视频文件,子文件夹下面有视频文件或者文件夹 现在因为需要转码,转码软件只能对单个 ...

  3. [转]linux下编译boost.python

    转自:http://blog.csdn.net/gong_xucheng/article/details/25045407 linux下编译boost.python 最近项目使用c++操作python ...

  4. SpringCloud和Springboot

    SpringBoot+SpringCloud+SpringMVC+SpringData 我们把这种架构也称之为spring全家桶 什么是SpringCloudSpring Cloud是一系列框架的有序 ...

  5. oracle数据库报错ora-01653表空间扩展失败解决方案

    1)ora-01653错误截图: 可以看到有两张表的insert受到了影响,都是在USERS表空间里.用以下SQL查看表空间使用情况: "表空间大小(M)",(a.bytes &q ...

  6. 记一次KUBERNETES/DOCKER网络排障

    https://coolshell.cn/articles/18654.html 总结在前面: 1.kill -9杀死docker进程,系统一定是要遍历所有的docker子进程来一个一个发退出信号的, ...

  7. WPF DEV dxc:ChartControl 柱状图

    先上效果图: <UserControl xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts" mc ...

  8. android 开发 View _8_ 动态图片自定义View

    转载地址:https://blog.csdn.net/mengks1987/article/details/77770922 先来看下效果: 是不是有一种熟悉感,其实这种效果使用序列帧动画也是可以实现 ...

  9. centos密码策略

    centos7密码策略 https://blog.csdn.net/qq_36896749/article/details/80264280 centos7设置密码规则 https://blog.cs ...

  10. javaweb项目编译错误

    Eclipse Maven 开发一个 jee 项目时,编译时遇到以下错误:Description Resource Path Location TypeDynamic Web Module 3.0 r ...