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. Java异常之try,catch,finally,throw,throws

    Java异常之try,catch,finally,throw,throws 你能区分异常和错误吗? 我们每天上班,正常情况下可能30分钟就能到达.但是由于车多,人多,道路拥挤,致使我们要花费更多地时间 ...

  2. weex APIs

    1.通过这个$vm()上下文访问这些api在脚本的方法 <script> module.exports = { methods: { somemethod: function() { th ...

  3. DNS服务器的原理

    当用户去访问一个网站(百度)的时候,首先去请求dns服务器,根据对应的域名返回所在ip,然后再使用ip去访问自己所在服务器空间.简单的说,DNS服务器就像114客服,dns服务器是树状结构的,dns服 ...

  4. java_Collection 类集

    大体概念

  5. GridView中某一列值的总和(web)

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)    {        if (e.Row.R ...

  6. oracle 中查看一张表是否有主键,主键在哪个字段上的语句怎么查如要查aa表,

    select a.constraint_name, a.column_name from user_cons_columns a, user_constraints b where a.constra ...

  7. Sublime Text 3运行JavaScript控制台

    Node.js是一个基于Chrome JavaScript运行时建立的平台,小巧方便搭建.运行的端口可以在浏览器上运行,显示效果,但每次用浏览器也挺麻烦,我们这里讲的是在sublime text2中配 ...

  8. 【POJ1568】【极大极小搜索+alpha-beta剪枝】Find the Winning Move

    Description 4x4 tic-tac-toe is played on a board with four rows (numbered 0 to 3 from top to bottom) ...

  9. c 连接数据库 mysql

    sudo apt-get install mysql-server mysql-client 再装开发包代码:sudo apt-get install libmysqlclient15-dev 安装完 ...

  10. rel=nofollow 是什么意思

    nofollow是什么意思? nofollow是html标签的一个属性值,Google推荐使用nofollow,告诉机器(爬虫)无需追踪目标页,是指禁止蜘蛛爬行和传递权重,但是如果你是通过sitema ...