描述

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. 使用Duilib开发Windows软件(2)——控件的基本介绍

    XML 先学习xml https://www.w3cschool.cn/xml/xml-xml-tutorialhc4o1t5m.html 控件 上图是我们下载的NIM_Duilib_Framewor ...

  2. 【爬虫集合】Python爬虫

    一.爬虫学习教程 1. https://www.jianshu.com/u/c32d557edfa3 2. WebMagic是一个简单灵活的Java爬虫框架.基于WebMagic,你可以快速开发出一个 ...

  3. (七)发送、接收SOAP消息(以HttpClient方式)(2)

    一.为什么要用soap 原本我们使用web服务都是根据wsdl生成客户端(生成一堆java文件)然后再调用,本章节讲解如何用soap消息来替代这种方式. 二.SOAP消息格式 SOAP(简单对象访问协 ...

  4. Springcloud 引导上下文

    SpringCloud为我们提供了bootstrap.properties的属性文件,我们可以在该属性文件里做我们的服务配置.可是,我们知道SpringBoot已经为我们提供了做服务配置的属性文件ap ...

  5. Sql Server根据字符串拆分多列

    select parsename(REPLACE(Field,',','.'),3) as segment_1, parsename(REPLACE(Field,',','.'),2) as segm ...

  6. C# 中类的成员有哪些?

    类(class)是C#类型中最基础的类型.类是一个数据结构,将状态(字段)和行为(方法和其他函数成员)组合在一个单元中.类提供了用于动态创建类实例的定义,也就是对象(object).类支持继承(inh ...

  7. 使用百度echarts仿雪球分时图(一)

    第一次写技术博客,有不足的地方希望大家指证出来,我再加以改正,谢谢大家. 之前一直没有找到一个合适的分时图项目,所以决定自己动手撸一个.接触的图表框架不多,在网上看到不少人推荐使用echarts,看了 ...

  8. ES6 Proise 简单理解

    Promise 这是ES6中增加的一个处理异步的对象. 传统变成写异步函数的时候,经常会遇到回调套回调: Promise 是异步编程的一种解决方案,比传统的解决方案 -----回调函数和事件----- ...

  9. KVM之virsh管理虚拟机网卡配置

    虚拟机网卡管理 virsh attach-interface 添加网卡: [root@ubuntu ~]# virsh domiflist CentOS-V6.5.23-server01 Interf ...

  10. 6.NIO2-Path、Paths、Files

    NIO.2 jdk1.7中,java对 NIO 极大的扩展,主要增强的是对文件处理 和 文件系统特性的支持 关于其中一些API的使用 public class TestNIO_2_Path_File ...