描述

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.

分析

如果允许重复,那么上一题中“若A[first] <= A[mid],则[first,mid]区间有序”的假设就不成立了,比如可能存在旋转后形如[1,3,1,1,1]的数组。

显然,如果A[first]<=A[mid]不能确定是否有序,就把这个条件细分一下:

  • 若A[first] < A[mid],就一定递增;
  • 若A[first] == A[mid],则确定不了,就将first加1,往下看一步即可。

代码如下:

class Solution{
public:
int search(int A[],int n,int target){
int first = 0,last = n;
const int mid = (first + mid) / 2; //note that mid is const,which means that we would not modify the value of mid before a new loop starts
while(first != mid){
if(A[mid] == target)
return mid;
if(A[first] < A[mid]){
if(A[first] <= target && target < A[mid])
last = mid;
else
first = mid + 1;
}
else if(A[mid] == A[first])
++first; //skip duplicatted one
else{
if(A[mid] <= target && target <= A[last - 1])
first = mid + 1;
else
last = mid;
}
}
return -1;
}
}

leetcode解题报告(4):Search in Rotated Sorted ArrayII的更多相关文章

  1. Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII

    一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码 ...

  2. leetcode 二分查找 Search in Rotated Sorted ArrayII

    Search in Rotated Sorted Array II Total Accepted: 18500 Total Submissions: 59945My Submissions Follo ...

  3. leetcode第32题--Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  4. LeetCode 笔记系列九 Search in Rotated Sorted Array

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  5. LeetCode(33)Search in Rotated Sorted Array

    题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 m ...

  6. LeetCode解题报告—— Word Search & Subsets II & Decode Ways

    1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...

  7. leetcode个人题解——#33 Search in Rotated Sorted Array

    思路:每次取中间元素,一定有一半有序,另一半部分有序,有序的部分进行二分查找,部分有序的部分递归继续处理. class Solution { public: ; int middleSearch(in ...

  8. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

  9. LeetCode: Search in Rotated Sorted Array 解题报告

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

随机推荐

  1. Hibernate一对多自关联、多对多关联

    今天分享hibernate框架的两个关联关系    多对多关系注意事项 一定要定义一个主控方 多对多删除 主控方直接删除 被控方先通过主控方解除多对多关系,再删除被控方 禁用级联删除 关联关系编辑,不 ...

  2. 夯实基础:彻底搞清楚Cookie 和 Session 关系和区别(转)

    原文地址:http://www.sohu.com/a/281228178_120047080 网络请求中的cookie与set-Cookie的交互模式和作用:https://my.oschina.ne ...

  3. 怎样理解window.name

    window.name表示当前窗口的名字, 而非网页的名字, 网页的名字需要使用: document.title; window.name一般是空的字符串, 他的作用其实是配合配合超链接和表单的tar ...

  4. Linux查看进程并重启服务命令

    top -u root 查看系统进程service network restartservice iptables restartservice sshd restartservice nginx r ...

  5. xcode 更改svn地址

    xcode如何更改svn地址: 1在控制台 cd进入到项目路径下 命令提示符下输入 $svn info 查看svn信息 修改svn地址 输入如下命令 $svn switch --relocate ht ...

  6. 补充:Python安装

    需要安装Python2.7.Numpy和Matplotlib.由于Python不支持向下兼容,因此在Python3.×下你一定能正常运行Python2.×的代码.上述模块最简单的安装方法就是用软件包安 ...

  7. httpd源码编译安装

    什么是编译安装——编译:将源代码变为机器可执行的代码文件.安装:将可执行文件安装到操作系统里,才可以使用. 一.下载httpd源码包 在官网上下载httpd源码包http://httpd.apache ...

  8. python 爬虫 Selenium的简单使用

    一.Selenium基础介绍及安装 1.Selenium简介 Selenium是一个用于测试网站的自动化测试工具,支持各种浏览器包括Chrome.Firefox.Safari等主流界面浏览器,同时也支 ...

  9. Install RabbitMQ on CentOS 7

    NOTE: this article is only for CentOS 7 How to Install RabbitMQ on CentOS 7 yum update Install erlan ...

  10. openpose-opencv 的body数据多人体姿态估计

    介绍 opencv除了支持常用的物体检测模型和分类模型之外,还支持openpose模型,同样是线下训练和线上调用.这里不做特别多的介绍,先把源代码和数据放出来- 实验模型获取地址:https://gi ...