Java for LeetCode 081 Search in Rotated Sorted Array II
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的更多相关文章
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [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. 思路 ...
- 【leetcode】Search in Rotated Sorted Array II
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- [LeetCode]题解(python):081 - Search in Rotated Sorted Array II
题目来源 https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ Follow up for "Search in ...
- 【LeetCode】081. Search in Rotated Sorted Array II
题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...
- LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【LeetCode】Search in Rotated Sorted Array II(转)
原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...
- [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. ...
随机推荐
- iOS进行单元测试OCUnit+xctool
单元测试 什么是单元测试 wiki解释 简单说来就是为你的方法多专门写一个测试函数.以保证你的方法在不停的修改开发中.保持正确.如果出错,第一时间让你知道,这样从最小单位开始监控来保证软件的质量. 我 ...
- MetaQ对接SparkStreaming示例代码
由于JavaReceiverInputDStream<String> lines = ssc.receiverStream(Receiver<T> receiver) 中 没有 ...
- Shell脚本部分语法
Shell中的变量 Linux Shell中的变量分为“系统变量”和“用户自定义变量”,可以通过set命令查看那系统变量 系统变量:$HOME.$PWD.$SHELL.$USER等等 显示当前sh ...
- 转: php实现的开源电商网站
转:http://www.magentochina.org/what-is-magento
- RPi Cam v2 之一:基础及牛刀小试
前言 原创文章,转载引用务必注明链接,水平有限,如有疏漏,欢迎指正. 本文使用markdown写成,为获得更好的阅读体验,可以访问我的博客. 1.unboxing & comparison 包 ...
- ios 之 autoresizing小解
对于IOS的app开发人员来说,不会像Android开发人员一样为非常多的屏幕尺寸来做界面适配,因此硬编码的坐标也能工作良好,可是从设计模式上来说这不是好的做法. 并且也另一些问题,如iPhone5的 ...
- iOS的URLScheme
一直都有接触要设置app的url scheme,从最早的facebook開始. 当时的理解是SSO用的,当授权成功之后,facebook app或者safari能够利用给定的url scheme来回调 ...
- apue学习笔记(第十三章 守护进程)
本章将说明守护进程结构,以及如何编写守护进程程序. 守护进程,也就是通常说的Daemon进程,是Unix中的后台服务进程.它是一个生存期较长的进程,通常独立于控制终端并且周期性地执行某种任务或等待处理 ...
- js:"use strict"; 严格模式
http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html
- swift菜鸟入门视频教程-05-控制流
本人自己录制的swift菜鸟入门.欢迎大家拍砖.有什么问题能够在这里留言. 主要内容: For 循环 While 循环 条件语句 控制转移语句(Control Transfer Statements) ...