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的更多相关文章

  1. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

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

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

  3. LeetCode——Search in Rotated Sorted Array II

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

  4. [leetcode]Search in Rotated Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...

  5. [LeetCode] Search in Rotated Sorted Array II [36]

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

  6. [Leetcode] search in rotated sorted array ii 搜索旋转有序数组

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

  7. [LeetCode] 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 -- 有重复的旋转序列搜索

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

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

  10. LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)

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

随机推荐

  1. 如何让Button使用自定义icon

    1.在Buttton所在的html页面定义button要使用的icon的css样式,如 </style> <style> .dijitArrowIcon { backgroun ...

  2. AudioUnit 用法

    1.描述音频单元 AudioComponentDescription desc; desc.componentType = kAudioUnitType_Output; desc.componentS ...

  3. C#多线程开发中如何更新UI界面控件内容

    子线程不能修改UI线程的状态(比如文本框里面的内容). 解决的办法是写一个用来更新文本框内容的函数,然后在Worker线程里面通过BeginInvoke来利用delegate调用这个函数更新文本框. ...

  4. Ionic2 rc2 Events 跨界面调用的使用方法及问题解决

    Events:事件注册  在任意一界面注册事件之后可在任意界面根据事件名称进行调用. 首先看一下官方文档的调用方式 import { Events } from 'ionic-angular'; co ...

  5. JS总结 本地对象2 BOM DOM

    string对象 定义一个字符: var str = "world"; str.length    返回字符长度,5 str.charAt(2) 返回字符串第2个字符(从0开始,下 ...

  6. python基础之内置函数

    该博客内容参考http://www.cnblogs.com/wupeiqi/articles/4943406.html 内置函数 一 详细见python文档,猛击这里 文件操作 操作文件时,一般需要经 ...

  7. Python学习之变量

    Python 变量 python不用事先声明变量,赋值过程中就包含了变量声明和定义的过程 用“=”赋值,左边是变量名,右边是变量的值 数字 整数 int_var = 1 长整数 long_var = ...

  8. Jsonp理论实例代码详解

    什么是Json?JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - ...

  9. c++11新的小猫腻

    1.void*指针的使用,平时见得也很多了,至于为什么使用void* 指针,很多人有自己的见解,反正普通指针轻轻松松的转向void * 指针,但是void*指针转向其他的指针都要采用强制转换的. 2. ...

  10. c++ float能到小数点后多少位

    float xiaoshu=0.0000000000000000000000000000000000000000000001; cout<<"xiaoshu"<& ...