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

public class Solution {public boolean search(int[] A, int target) {return search(A, target, 0, A.length-1);}private boolean search(int[] A, int target, int left, int right){if( left > right) return false;if (left == right) return (A[left] == target);if (A[left] < A[right] && (target < A[left] || target > A[right])) return false;if (A[left] > A[right] && target > A[right] && target < A[left]) return false;int mid = left + (right - left) / 2;if (A[mid] == target) return true;else{return search(A, target, left, mid-1) || search(A, target, mid+1, right);}}
}

网上有个思路更简洁,二分。如果相等导致不能二分,则直接跳过这个重复的元素。

class Solution {
public:bool search(int A[], int n, int key) {int l = 0, r = n - 1;while (l <= r) {int m = l + (r - l)/2;if (A[m] == key) return true; //return m in Search in Rotated Array Iif (A[l] < A[m]) { //left half is sortedif (A[l] <= key && key < A[m])r = m - 1;elsel = m + 1;} else if (A[l] > A[m]) { //right half is sortedif (A[m] < key && key <= A[r])l = m + 1;elser = m - 1;} else l++;}return false;}
};

  

转载于:https://www.cnblogs.com/lautsie/p/3356200.html

[leetcode]Search in Rotated Sorted Array II相关推荐

  1. LeetCode Search in Rotated Sorted Array II

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

  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(middle)☆

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

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

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

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

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

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

  8. 81. Search in Rotated Sorted Array II

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

  9. Search in Rotated Sorted Array II

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

最新文章

  1. 使用Spring容器
  2. 百度正在摆脱广告营收依赖!AI云增长64%成最大推动力
  3. 学习python的日常6
  4. macos安装homebrew
  5. Windows Azure Storage (19) 再谈Azure Block Blob和Page Blob
  6. [WPF]WPF中材质制作——图片和矢量图之争
  7. python函数调用的三种方式_判断python对象是否可调用的三种方式及其区别
  8. SpringBoot启动项目时提示:Error:(3, 32) java: 程序包org.springframework.boot不存在
  9. 界面设计语言_使用任何语言设计界面的提示
  10. python快捷方式图标_python – PyInstaller无法更改快捷方式图标
  11. Linux 中 VIM 的使用
  12. Web前端开发工程师必读de设计博客
  13. 如何让字体大小12px
  14. [Linux]常用命令与目录全拼
  15. 苹果计算机 win10,苹果电脑怎么安装Win10系统?
  16. excel表格快捷键
  17. 实验3:利用SVM实现线性高斯分类
  18. 运筹说 第92期|爱思唯尔“高被引学者“
  19. Windows 10 使用小鹤双拼
  20. 2D 23.2.23

热门文章

  1. employees mysql_「employees」mysql示例employees数据库 - seo实验室
  2. LSH︱python实现局部敏感哈希——LSHash(二)
  3. [NLP]OpenNLP语言检测器的使用
  4. [Flink]Flink DataStream window join 和interval join
  5. (17/24) webpack实战技巧:生产环境和开发环境并行设置,实现来回切换
  6. 洛谷 1131 [ZJOI2007]时态同步——树形dp
  7. 在CentOS 6.7部署wordpress博客系统Discuz论坛系统
  8. “提速降费” 并非一蹴而就 矛头齐指运营商有失偏颇
  9. Struts(十二):异常处理:exception-mapping元素
  10. ATA/SATA/SCSI/SAS/FC总线简介