原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/

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

这道题是二分查找Search Insert Position的变体,思路在Search in Rotated Sorted Array中介绍过了,不了解的朋友可以先看看那道题哈。和Search in Rotated Sorted Array唯一的区别是这道题目中元素会有重复的情况出现。不过正是因为这个条件的出现,出现了比较复杂的case,甚至影响到了算法的时间复杂度。原来我们是依靠中间和边缘元素的大小关系,来判断哪一半是不受rotate影响,仍然有序的。而现在因为重复的出现,如果我们遇到中间和边缘相等的情况,我们就丢失了哪边有序的信息,因为哪边都有可能是有序的结果。假设原数组是{1,2,3,3,3,3,3},那么旋转之后有可能是{3,3,3,3,3,1,2},或者{3,1,2,3,3,3,3},这样的我们判断左边缘和中心的时候都是3,如果我们要寻找1或者2,我们并不知道应该跳向哪一半。解决的办法只能是对边缘移动一步,直到边缘和中间不在相等或者相遇,这就导致了会有不能切去一半的可能。所以最坏情况(比如全部都是一个元素,或者只有一个元素不同于其他元素,而他就在最后一个)就会出现每次移动一步,总共是n步,算法的时间复杂度变成O(n)。代码如下:

  1. public boolean search(int[] A, int target) {
  2. if(A==null || A.length==0)
  3. return false;
  4. int l = 0;
  5. int r = A.length-1;
  6. while(l<=r)
  7. {
  8. int m = (l+r)/2;
  9. if(A[m]==target)
  10. return true;
  11. if(A[m]>A[l])
  12. {
  13. if(A[m]>target && A[l]<=target)
  14. {
  15. r = m-1;
  16. }
  17. else
  18. {
  19. l = m+1;
  20. }
  21. }
  22. else if(A[m]<A[l])
  23. {
  24. if(A[m]<target && A[r]>=target)
  25. {
  26. l = m+1;
  27. }
  28. else
  29. {
  30. r = m-1;
  31. }
  32. }
  33. else
  34. {
  35. l++;
  36. }
  37. }
  38. return false;
  39. }

以上方法和Search in Rotated Sorted Array是一样的,只是添加了中间和边缘相等时,边缘移动一步,但正是这一步导致算法的复杂度由O(logn)变成了O(n)。个人觉得在面试中算法复杂度还是很重要的考察点,因为涉及到对算法的理解,大家还是要尽量多考虑哈。

【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. Windows Builder(图形化界面的利器)For Eclipse 3.7

    工欲善其事,必先利其器——孔子(春秋)<论语·卫灵公> 今天闲逛论坛的时候,发现了Eclipse 的很好的插件,是关于做图形界面的. 如果想做桌面应用软件,交互界面有点复杂的时候,自己手动 ...

  2. Codeforces 622C Not Equal on a Segment 【线段树 Or DP】

    题目链接: http://codeforces.com/problemset/problem/622/C 题意: 给定序列,若干查询,每个查询给定区间和t,输出区间内任意一个不等于t的元素的位置. 分 ...

  3. 2017 ACM-ICPC 北京区域赛记录

    ------------------------------------------------------------------------------ 出发日 拖着一个大箱子走是真的累. 下午三 ...

  4. vue报错之Duplicate keys detected: '0'. This may cause an update error.

    昨天运行vue项目的时候,出现了[Vue warn]: Duplicate keys detected: '0'. This may cause an update error(错误,检测到重复的ke ...

  5. 2017-11-07-noip模拟题

    T1 数学老师的报复 矩阵快速幂模板,类似于菲波那切数列的矩阵 [1,1]*[A,1 B,0] #include <cstdio> #define LL long long inline ...

  6. 洛谷—— P2251 质量检测

    https://www.luogu.org/problemnew/show/P2251 题目背景 无 题目描述 为了检测生产流水线上总共N件产品的质量,我们首先给每一件产品打一个分数A表示其品质,然后 ...

  7. REBXOR

    题面 Description 给定一个含N个元素的数组A,下标从1开始.请找出下面式子的最大值. (A[l1]xorA[l2+1]xor-xorA[r1])+(A[l2]xorA[l2+1]xor-x ...

  8. zerorpc的安装

    1.简介及安装 rpc使构建分布式系统简单许多,在云计算的实现中有很广泛的应用 rpc可以是异步的 python实现rpc,可以使用标准库里的SimpleXMLRPCServer,另外zerorpc是 ...

  9. 很多shell命令后面的单横杠和双横杠,原来这个意思

    原文: https://blog.csdn.net/deyili/article/details/5471023 ------------------------------------------- ...

  10. VueJS样式绑定:v-bind

    HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...