Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).

You are given a target value to search. If found in the array return true, otherwise return false.

Example 1:

  1. Input: nums = [2,5,6,0,0,1,2], target = 0
  2. Output: true

Example 2:

  1. Input: nums = [2,5,6,0,0,1,2], target = 3
  2. Output: false

Follow up:

  • This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
  • Would this affect the run-time complexity? How and why?

这道是之前那道 Search in Rotated Sorted Array 的延伸,现在数组中允许出现重复数字,这个也会影响我们选择哪半边继续搜索,由于之前那道题不存在相同值,我们在比较中间值和最右值时就完全符合之前所说的规律:如果中间的数小于最右边的数,则右半段是有序的,若中间数大于最右边数,则左半段是有序的。而如果可以有重复值,就会出现来面两种情况,[3 1 1] 和 [1 1 3 1],对于这两种情况中间值等于最右值时,目标值3既可以在左边又可以在右边,那怎么办么,对于这种情况其实处理非常简单,只要把最右值向左一位即可继续循环,如果还相同则继续移,直到移到不同值为止,然后其他部分还采用 Search in Rotated Sorted Array 中的方法,可以得到代码如下:

  1. class Solution {
  2. public:
  3. bool search(vector<int>& nums, int target) {
  4. int n = nums.size(), left = , right = n - ;
  5. while (left <= right) {
  6. int mid = (left + right) / ;
  7. if (nums[mid] == target) return true;
  8. if (nums[mid] < nums[right]) {
  9. if (nums[mid] < target && nums[right] >= target) left = mid + ;
  10. else right = mid - ;
  11. } else if (nums[mid] > nums[right]){
  12. if (nums[left] <= target && nums[mid] > target) right = mid - ;
  13. else left = mid + ;
  14. } else --right;
  15. }
  16. return false;
  17. }
  18. };

Github 同步地址:

https://github.com/grandyang/leetcode/issues/81

类似题目:

Search in Rotated Sorted Array

参考资料:

https://leetcode.com/problems/search-in-rotated-sorted-array-ii/

https://leetcode.com/problems/search-in-rotated-sorted-array-ii/discuss/28194/C%2B%2B-concise-log(n)-solution

https://leetcode.com/problems/search-in-rotated-sorted-array-ii/discuss/28218/My-8ms-C%2B%2B-solution-(o(logn)-on-average-o(n)-worst-case)

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二的更多相关文章

  1. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II

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

  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 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)

    题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description   姊妹篇:http://www. ...

  4. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

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

  5. LeetCode 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  6. [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

      Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i. ...

  7. leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法

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

  8. [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II

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

  9. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

随机推荐

  1. ICT638 Mobile and App Development

    Assessment Cover SheetStudent ID CohortStudent NameProgrammeEnrolledDiploma in Information Technolog ...

  2. Redis(七)分布式锁

    前面学习了Redis的数据结构以及命令.Redis中的事务和Redis对Lua脚本的支持. 这一章就对Redis这些特性做一下实战性应用--基于Redis的分布式锁实现. Lock和Distribut ...

  3. Asp.NET Core Nginx Ocelot ForwardedHeaders X-Forwarded-For

    ocelot在部署时我使用了nginx作为转发,并配置了https证书,但是发现ocelot不支持Forward host header. https://ocelot.readthedocs.io/ ...

  4. dedecms5.7文章页替换掉特定标志的图片链接

    dedecms5.7文章页的替换掉特定标志的图片链接 解决思路 1个是在数据库里面执行替换操作 我自己查看 织梦后台也有这个功能  但是执行了一次 效果不是很好  那么就用下面的  在模板中进行内容替 ...

  5. 【机器学习笔记】ID3构建决策树

    好多算法之类的,看理论描述,让人似懂非懂,代码走一走,现象就了然了. 引: from sklearn import tree names = ['size', 'scale', 'fruit', 'b ...

  6. sql2008好看的字体

  7. 快速上手Mac效率神器Alfred以及Alfred常用操作

    前言 Alfred,想必大家就算没用过也耳闻过.Alfred是一个让你可以丢掉鼠标的神器.很多读者可能之前认为Alfred的学习成本高,或者感觉它太复杂,而望之却步.其实Alfred并非高不可攀,本文 ...

  8. ios浏览器调试踩坑(1)----mescroll.js和vue-scroller

    主要记录在ios浏览器出现触摸无限加载的情况 使用vue-scroller和mescroll.js/mescroll.vue先踩ios浏览器默认滑动会影响mescroll的方法调用. 首先给公共js加 ...

  9. SPC软控件提供商NWA的产品在各行业的应用(生命科学行业)

    在上一篇文章中,我们提到了NWA软件产品在各行业都有广泛的应用,并且就化工行业的应用展开了详细介绍.而在本文中,您将看到NWA产品在生命科学行业也扮演着不可替代的角色. Northwest Analy ...

  10. ConstraintLayout 用法

    当前描述是基于constraint-layout:1.1.2. 一.前言 在以前,android是使用布局如LinearLayout .RelativeLayout等来构建页面,但这些布局使用起来很麻 ...