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. js时间字符串转Date对象

    var DATE_REGEXP = new RegExp("(\\d{4})-(\\d{2})-(\\d{2})([T\\s](\\d{2}):(\\d{2}):(\\d{2})(\\.(\ ...

  2. solve_lock-1024-大功告成

    create or replace procedure solve_lock_061203(v_msg out varchar2) as  v_sql varchar2(3000); --定义 v_s ...

  3. PictureBox内的图片拖动功能

    当 PictureBox内的图片太大,超过PictureBox边框时可以用下面的方法来实现,   通过重绘来实现 :   Code bool wselected = false;  Point p = ...

  4. 用html/css做的一个登入小界面(图片瀑布流)

    一个登入效果简易图:(色彩搭配有点乱,嘻嘻,可以在代码处改成自己喜欢的颜色) css样式的代码: style.css: @charset "utf-8";/* CSS Docume ...

  5. hdoj1847(博弈论)

    代码: #include<stdio.h>int main(){ int N; while(scanf("%d",&N)!=EOF) printf(N%3==0 ...

  6. 104. Maximum Depth of Binary Tree(C++)

    104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...

  7. removing right click context menu options on recycle bin

    Humpty is correct as always  First you might want to make a backup of the reg key then remove the Wa ...

  8. Linux系统、版本、CPU、内存查看、硬盘空间

    查看系统版本:lsb_release -a [root@localhost /]# lsb_release -a LSB Version:    :core-4.0-amd64:core-4.0-no ...

  9. 《zip命令》-linux命令五分钟系列之九

    本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...

  10. 实例:用jQuery实现垂直和水平下拉 菜单

    主要是利用jQuery来实现垂直菜单和水平菜单.实现效果如图: 第一步,创建一个HTML文件,如图,包含两个ul.当然把jquery库也引入进去了. <!DOCTYPE html> < ...