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

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

解题思路:

参考Java for LeetCode 033 Search in Rotated Sorted Array修改下代码即可,JAVA实现如下:

    public boolean search(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
if (target == nums[(right + left) / 2])
return true;
// 右半部分为旋转区域
if (nums[(right + left) / 2] > nums[left]) {
if (target >= nums[left] && target < nums[(right + left) / 2])
right = (right + left) / 2 - 1;
else
left = (right + left) / 2 + 1;
}
// 左半部分为旋转区域
else if (nums[(right + left) / 2] < nums[left]) {
if (target > nums[(right + left) / 2] && target <= nums[right])
left = (right + left) / 2 + 1;
else
right = (right + left) / 2 - 1;
}
// 老实遍历
else
left++;
}
return false;
}

Java for LeetCode 081 Search in Rotated Sorted Array 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旋转过有序数组里找目标值II(有重)

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

  4. 【leetcode】Search in Rotated Sorted Array II

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

  5. [LeetCode]题解(python):081 - Search in Rotated Sorted Array II

    题目来源 https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ Follow up for "Search in ...

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

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

  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】Search in Rotated Sorted Array II(转)

    原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...

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

随机推荐

  1. 避免在block中循环引用(Retain Cycle in Block)

    让我们长话短说.请参阅如下代码: - (IBAction)didTapUploadButton:(id)sender { NSString *clientID = @"YOUR_CLIENT ...

  2. Android中的多线程断点续传

    Android多线程断点下载的代码流程解析: 运行效果图: 实现流程全解析: Step 1:创建一个用来记录线程下载信息的表 创建数据库表,于是乎我们创建一个数据库的管理器类,继承SQLiteOpen ...

  3. 【POI】导出xls文件报错:The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook

    使用POI导出xls文件,由于数据过多,导致导出xls报错如下: The maximum number of cell styles was exceeded. You can define up t ...

  4. eclipse中修改JDK版本

    eclipse中,一个java项目依赖的JDK,需要进行绑定,但绑定的地方会有多个,类似层级结构. 1. eclipse的window -> preferences -> java com ...

  5. Activity的onRestart()方法调用时机

    在项目中看到Activity使用onRestart(),平时对这个方法的调用时机知道的比较少,研究一下它啥时候会被调用. 代码很简单: <span style="font-size:1 ...

  6. 【转】如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等

    如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等   你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或在里面搜索,或其它操作——一些无法并 ...

  7. oracle exchange partition 測试

    Exchange partition提供了一种方式,让你在表与表或分区与分区之间迁移数据.注意不是将表转换成分区或非分区的形式,而仅仅仅是迁移表中数  据(互相迁移),因为其号称是採用了更改数据字典的 ...

  8. curses.h头文件不存在解决办法

    sudo apt-get install libncurses5-dev安装,系统自带库文件一般在/usr/include下面,这个是安装curses.h的 conio不是c语言标准库,也不是posi ...

  9. imagemagick imagick

    imagemagick#图像处理软件 安装解压 ./configure make make install imagick#是php图像扩展模块 调用imagemagick处理图像 安装解压/opt/ ...

  10. http Keep-Alive

    1.什么是Keep-Alive模式? 我们知道HTTP协议采用“请求-应答”模式,当使用普通模式,即非KeepAlive模式时,每个请求/应答客户和服务器都要新建一个连接,完成之后立即断开连接(HTT ...