题目:

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.

题解:

这道题与之前Search in Rotated Sorted Array类似,问题只在于存在dupilcate。那么和之前那道题的解法区别就是,不能通过比较A[mid]和边缘值来确定哪边是有序的,会出现A[mid]与边缘值相等的状态。所以,解决方法就是对于A[mid]==A[low]和A[mid]==A[high]单独处理。

当中间值与边缘值相等时,让指向边缘值的指针分别往前移动,忽略掉这个相同点,再用之前的方法判断即可。

这一改变增加了时间复杂度,试想一个数组有同一数字组成{1,1,1,1,1},target=2, 那么这个算法就会将整个数组遍历,时间复杂度由O(logn)升到O(n)

实现代码如下:

 1     public boolean search(int [] A,int target){
 2        if(A==null||A.length==0)
 3          return false;
 4         
 5        int low = 0;
 6        int high = A.length-1;
 7       
 8        while(low <= high){
 9            int mid = (low + high)/2;
10            if(target < A[mid]){
11                if(A[mid]<A[high])//right side is sorted
12                  high = mid - 1;//target must in left side
13                else if(A[mid]==A[high])//cannot tell right is sorted, move pointer high
14                  high--;
15                else//left side is sorted
16                  if(target<A[low])
17                     low = mid + 1;
18                  else 
19                     high = mid - 1;
20            }else if(target > A[mid]){
21                if(A[low]<A[mid])//left side is sorted
22                  low = mid + 1;//target must in right side
23                else if(A[low]==A[mid])//cannot tell left is sorted, move pointer low
24                  low++;
25                else//right side is sorted
26                  if(target>A[high])
27                     high = mid - 1;
28                  else
29                     low = mid + 1;
30            }else
31              return true;
32        }
33        
34        return false;
35 } 

Reference: http://blog.csdn.net/linhuanmars/article/details/20588511

转载于:https://www.cnblogs.com/springfor/p/3859525.html

Search in Rotated Sorted Array II leetcode java相关推荐

  1. leetcode 81 Search in Rotated Sorted Array II ----- java

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

  2. 【leetcode】Search in Rotated Sorted Array II(middle)☆

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

  4. LeetCode Search in Rotated Sorted Array II -- 有重复的旋转序列搜索

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

  5. LeetCode Online Judge 题目C# 练习 - 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

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

  7. 81. Search in Rotated Sorted Array II

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

  8. [算法题] Search in Rotated Sorted Array ii

    题目内容 题目来源:LeetCode Suppose an array sorted in ascending order is rotated at some pivot unknown to yo ...

  9. Search in Rotated Sorted Array II

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

  10. [leetcode]Search in Rotated Sorted Array II

    这道题目仍然是二分,去掉不可能的部分.用了递归,在重复的情况下,就是有可能最左边的和最右边的相等,此时就不能直接判断出区间外的元素,左右两边同时递归.有重复元素的时候,在bad case的时候会退化为 ...

最新文章

  1. 力扣(LeetCode)刷题,简单题+中等题(第17期)
  2. 给Oracle表空间Tablespace扩展库文件
  3. html5标签 H5标签
  4. python的datetime模块需要装吗,Python datetime模块的介绍(日期格式化 时间戳)
  5. docker环境无法执行jmap -heap 56命令
  6. LR模型常见问题小议
  7. [Nginx]用Nginx实现与应用结合的訪问控制 - 防盗链
  8. ElasticSearch常用命令记录
  9. 关于slot、slot-scope的指令的一些操作记录
  10. .NET Core第三方开源Web框架YOYOFx
  11. 录制caf 转 mp3
  12. java stl分解_Java Thread 之間 記憶體空間分析
  13. SqlServer数据冗余的问题和解决
  14. MyBatis学习(四)MyBatis缓存
  15. SpringApplication.run 做了哪些事?
  16. Jmeter安装教程
  17. 微软放弃收购雅虎猜想:杨致远代价不菲的胜利
  18. BUAA_4:Kevin·Feng的正确@姿势
  19. 简单好用微服务套件AnnoViper DashBoard全新版来啦
  20. 《脉脉:人才流动与迁徙2022》,遭”哄抢”的复合型程序员成IT黑马

热门文章

  1. netty与socket对比_Netty之WebSocket和四种IO介绍
  2. Linux中级之lvs三个模式的图像补充(nat,dr,tun)
  3. JavaScript 字符串(String)对象
  4. 大数据 Spark 架构
  5. 用crontab命令实现每天定时的病毒扫描
  6. 百度地图之添加控件——比例尺、缩略图、平移缩放
  7. 编程菜鸟的日记-初学尝试编程递归
  8. zen-Coding
  9. TwoStep Cluster
  10. 中文问题-Mobile-UrlEncode