LeetCode-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.
解决方法就是对于A[mid]==A[left]和A[mid]==A[right]单独处理。
public class Solution {
public boolean search(int[] nums, int target) {
if(nums==null || nums.length==0){
return false;
}
int left=0;
int right=nums.length-1;
while(left<=right){
int mid=left+(right-left)/2;
if(target<nums[mid]){
if(nums[mid]< nums[right]){// right side is sorted
right=mid-1;
}
else if(nums[mid] == nums[right]){//can't tell right is sorted or not, move pointer
right--;
}
else {//left side is sorted
if(target<nums[left]){
left=mid+1;
}
else{
right=mid-1;
}
}
}
else if(target>nums[mid]){
if(nums[mid] > nums[left]){//left is sorted
left=mid+1;
}
else if(nums[mid]== nums[left]){// cann't tell left side is sorted or not, move pointer
left++;
}
else{//right is sorted
if(target>nums[right]){
right=mid-1;
}
else{
left=mid+1;
}
}
}
else{
return true;
}
}
return false;
}
}
LeetCode-Search in Rotated Sorted Array II的更多相关文章
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- [LeetCode] 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
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- [leetcode]Search in Rotated Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...
- [LeetCode] Search in Rotated Sorted Array II [36]
称号 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
- [Leetcode] 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 二分搜索
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode 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 I II
LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...
- LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- Oracle存储过程中异常Exception的捕捉和处理
Oracle存储过程中异常的捕捉和处理 CREATE OR REPLACE Procedure Proc_error_process ( v_IN in Varchar2, v_OUT Out Var ...
- XCode6.0的iOS免证书真机测试方法(MAC及黑苹果均有效)[转]
目前在XCode上开发的iOS程序只能在模拟器Simulator中运行,如果要放到真机上测试,需要苹果官方认证的开发者账号,购买开发者证书iDP,99美金一年啊!!! 作为刚开始学习iOS编程的菜鸟, ...
- Scala编程--函数式对象
本章的重点在于定义函数式对象,也就是说,没有任何可变状态的对象的类.作为运行的例子,我们将创造若干把分数作为不可变对象建模的类的变体.在这过程中,我们会展示给你Scala面向对象编程的更多方面:类参数 ...
- 使用javaScript实现简单倒计时功能
效果如下: <div class="warp"> <p id="txt">距离”十一“国庆放假还有:</p><br&g ...
- DateTime 详解
//2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...
- 摆花(2012Noip普及组第3题)
摆花 (flower.cpp/c/pas) [问题描述] 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共 m 盆.通过调查顾客的喜好,小明列出了顾客最喜欢的 n 种花,从 1 到 n ...
- get方式提交中文乱码解决
get方式提交中文时会乱码,过滤器只过滤post请求,此时可修改tomcat配置文件server.xml,为Connector添加属性URIEncoding="utf-8". ec ...
- Spark源码学习1.5——BlockManager.scala
一.BlockResult类 该类用来表示返回的匹配的block及其相关的参数.共有三个参数: data:Iterator [Any]. readMethod: DataReadMethod.Valu ...
- 关于最少VC号数目的猜想
[事先说明,实际的虚拟电路的实际物理链路可能同时具有多个VC号,但每段逻辑链路仅有一个VC号,一条完整虚拟路径由多个段组成] 问题描述: 在一个虚拟电路交换网络中,每个路由器的直连链路都有一个独一无二 ...
- java开发命名规范
使用前注意事项: 1. 由于Java面向对象编程的特性, 在命名时应尽量选择名词 2. 驼峰命名法(Camel-Case): 当变量名或函式名是由一个或多个单字连结在一起,而构成的唯一识别字时,首 ...