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.

这个题做了好长时间,到最后就这个想法。。。

后来看到别人真的做出来了,我于是又开始新的征程。。。终于顿悟了。。。

首先:

把左右两边的重复元素都过滤了。

while(lo<hi&&nums[lo]==nums[lo+1])lo++;
while(lo<hi&&nums[hi]==nums[hi-1])hi--;

然后开始思考一下rotated sorted array的特点:

有以下两种情况:

1 2 3 4 5 6 7 8 完全顺序的

5 6 7 8 1 2 3 4 反转的

lo = 0

hi = len - 1

mid = (lo+hi)>>>1

对于完全顺序的不用多说。

对于翻转的,这时mid会有两种情况:

一、nums[mid]>nums[hi]

二、nums[mid]<nums[hi]

ok,情况说明白了,下面来说target对应的情况:

如果target比nums[hi]大,那么在前半部分的情况有:

nums[hi]>nums[mid]或target<nums[mid]

如果target比nums[hi]小,那么在后部分的情况有:

target>nums[mid]或者nums[hi]<nums[mid]

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

Search in Rotated Sorted Array II——LeetCode的更多相关文章

  1. Search in Rotated Sorted Array II leetcode

    原题链接,点我 该题解题参考博客 和Search in Rotated Sorted Array唯一的区别是这道题目中元素会有重复的情况出现.不过正是因为这个条件的出现,出现了比较复杂的case,甚至 ...

  2. Search in Rotated Sorted Array II leetcode java

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

  3. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

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

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

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

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

  6. 【leetcode】Search in Rotated Sorted Array II

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

  7. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

  8. 【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 ...

  9. 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II

    33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...

随机推荐

  1. 前端自动化构建工具 Gulp 使用

    一个月没写博客了,今天有时间,就写个gulp的入门使用吧.. 简介:gulp是一个前端自动化构建工具,可以实现代码的检查.压缩.合并……等等,gulp是基于Node.js的自动任务运行器 一.安装No ...

  2. Hash - a javascript dictionary object.

    Hash,in wikipedia, may relevant to many stuffs. In javascript, hash is a group of name/value pairs w ...

  3. iOS单例的两种实现

    单例模式算是开发中比较常见的一种模式了.在iOS中,单例有两种实现方式(至少我目前只发现两种).根据线程安全的实现来区分,一种是使用@synchronized,另一种是使用GCD的dispatch_o ...

  4. cocos2dx ease 公式

    所有ease计算公式都在CCTweenFunction.cpp里.

  5. SGU 185.Two shortest (最小费用最大流)

    时间限制:0.25s 空间限制:4M 题意: 在n(n<=400)个点的图中,找到并输出两条不想交的最短路.不存在输出“No sulotion”: Solution: 最小费用最大流 建图与po ...

  6. Python下调用json.dumps中文显示问题解决办法

    json.dumps在默认情况下,对于非ascii字符生成的是相对应的字符编码,而非原始字符,例如: import json js = json.loads('{"haha": & ...

  7. Python中%s和%r的区别

    早先使用Python工作的时候,对于格式化输出%s和%r的使用都是混着用的. 这一次就出错了: cu.execute("insert into ipPool values(null, '%r ...

  8. CoreLocation MKMapView

    高德开发者平台 有开发指南 iOS9配置网络: <key>NSAppTransportSecurity</key> <dict> <key>NSAllo ...

  9. java枚举小结

    如何定义一个枚举类? //定义了4个等级 enum Level{ A,B,C,D } 枚举类的实质: class Level{ public static final Level A = new Le ...

  10. iOS9 以上的真机调试 不用证书

    具体流程如下: 首次使用AppleID  的注意事项:  要在设置中 进行 如下操作  设置--通用--描述文件   ---添加信任 但是有时候  还是 会不能调试,  显示信息  是这样的   : ...