Question:

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.

Tips:

给定一个数组,该数组是由一个有序的数组经过旋转(即将前面一段数字接到整个数组之后)得到的。判断target是否存在于该数组之中。

本题为33题升级版本,数组中的数字可以出现重复,如果target存在,返回他的true不存在则返回false。

思路:

本题与33提相似,但是由于数组中存在重复数字,可能会出现low high mid三个数字均相等的情况,这时为了跳出相等数字,需要low++或者high--;

代码:

 public boolean search(int[] nums, int target) {
if (nums == null)
return false;
int low = 0;
int len = nums.length;
int high = len - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == target)
return true;
if (nums[low] < nums[mid] || nums[mid]>nums[high]) {
if (target < nums[mid] && target >= nums[low]) {
high = mid - 1;
} else
low = mid + 1;
}else if(nums[mid]<nums[high] || nums[low]>nums[mid]){
if(target<=nums[high] && target>nums[mid]){
low=mid+1;
}else{
high=mid-1;
}
}
else{
low++;
}
}
return false;
}

【Leetcode】81. Search in Rotated Sorted Array II的更多相关文章

  1. 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  2. 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...

  3. 【一天一道LeetCode】#81. Search in Rotated Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  4. 【LeetCode】081. Search in Rotated Sorted Array II

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

  5. 【LeetCode】33. Search in Rotated Sorted Array (4 solutions)

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

  6. 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现

    一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...

  7. 【LeetCode】33. Search in Rotated Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【Leetcode】33. Search in Rotated Sorted Array

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

  9. 【LeetCode】033. Search in Rotated Sorted Array

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

随机推荐

  1. Python2.7-hashlib

    hashlib模块,实现了支持多种不同哈希算法的接口,不同 hash 算法的构造函数就是算法名,返回的哈希对象都具有相同接口.哈希算法不是加密算法,所以下面提到的加密不是真的加密,因为真的加密需要能够 ...

  2. JAVA框架 Mybaits

     注意:我们在resultType中,对于selectlist方法也是projo类.resultType参数的含义是list的泛型的类型. 一:jar包下载: https://github.com/m ...

  3. AMD、CMD和Common规范

    1.名词解释AMD:Asynchronous Modules Definition异步模块定义,提供定义模块及异步加载该模块依赖的机制.CMD:Common Module Definition 通用模 ...

  4. Arduino入门笔记(4):用蜂鸣器演奏音乐并配有LED闪烁

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 欢迎加入讨论群 64770604 一.本次实验所需器材 1.Arduino板 https://item.taoba ...

  5. 关于Altium Designer 提示发送错误报告解决方法

    提示是这样子,,,,,, 稍微有点问题就提示,,,复制也提示,,,,移动也提示,,,,,,算是服了这个软件了.......真是忍无可忍,那就无需再忍,解决掉 以前是安装上一个虚拟的打印机就好了,,,其 ...

  6. Advanced Electronic Engineer

    Job Title Advanced Electronic Engineer Job Description In this role, you have the opportunity to Be ...

  7. mac下载、破解、安装webstorm编辑器

    1.进入webstorm官网 http://www.jetbrains.com/webstorm/,点击DOWNLOAD,开始下载webstorm安装包. untitled.png 2.开始安装 双击 ...

  8. Scala_数据类型

    Scala与Java有着相同的数据类型,Scala数据类型都是对象,Scala中没有类似Java中那样的原始类型. Scala 的基本数据类型有: Byte,Short,Int,Long 和 Char ...

  9. 20155204《网络对抗》Exp 6 信息搜集与漏洞扫描

    20155204<网络对抗>Exp 6 信息搜集与漏洞扫描 一.实验后回答问题 1.哪些组织负责DNS,IP的管理. 互联网名称与数字地址分配机构,简称ICANN机构,决定了域名和IP地址 ...

  10. 【转】CentOS 5 上安装git

    转自 http://www.cnblogs.com/Neddy/archive/2011/02/28/1967548.html 注意安装的时候 都要以root身份 //先安装git依赖的包 yum i ...