描述

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[m]>=A[l]是无法判断出(m,l)之间是升序的。因此,需将大于和等于分开来考虑。

只需在等于时,将重复元素跳过再比较即可。

 int searchRotateSA(int A[], int n,int target)
{
int first = , last = n;
while (first!=last)
{
int mid = first + (last - first) / ;
if (A[mid] = target)
{
return mid;
}
else if (A[first]<A[mid])//判断大小顺序
{
if (A[first] <= target&&target <= A[mid])
last = mid;
else
first = mid + ;
}
else if (A[first]>A[mid])
{
if (A[first] >= target&& A[mid] <= target)
last = mid;
else
first = mid + ; }
else first++;
} return -;
}

leetcode 之Search in Rotated Sorted Array(四)的更多相关文章

  1. [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...

  2. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

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

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

  4. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

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

  5. [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)

    This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...

  6. Java for LeetCode 081 Search in Rotated Sorted Array II

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

  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系列-Search in Rotated Sorted Array

    做Leetcode题有一段时间了,但都是断断续续的,到现在才做了30题左右,感觉对自己来说还是有点难度的.希望自己能继续坚持下去,在校招前能解决超过一百题吧. 其实这些题就是用来训练你的解题思路的,做 ...

  9. LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)

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

随机推荐

  1. Mysql局域网访问授权

    如果允许用户myuser从ip为192.168.1.1的主机连接到mysql服务器,并使用password作为密码 GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'1 ...

  2. 网站统计IP PV UV实现原理

    网站流量统计可以帮助我们分析网站的访问和广告来访等数据,里面包含很多数据的,比如访问试用的系统,浏览器,ip归属地,访问时间,搜索引擎来源,广告效果等.原来是一样的,这次先实现了PV,UV,IP三个重 ...

  3. git用户名和邮箱配置

    1. 设置全局用户名和邮箱 git config --global user.name "xxx" git config --global user.email "xxx ...

  4. Tornado----自定义异步非阻塞Web框架:Snow

    Python的Web框架中Tornado以异步非阻塞而闻名.本篇将使用200行代码完成一个微型异步非阻塞Web框架:Snow. 一.源码 本文基于非阻塞的Socket以及IO多路复用从而实现异步非阻塞 ...

  5. 相机标定 和 单应性矩阵H

    求解相机参数的过程就称之为相机标定. 1.相机模型中的四个平面坐标系: 1.1图像像素坐标系(u,v) 以像素为单位,是以图像的左上方为原点的图像坐标系: 1.2图像物理坐标系(也叫像平面坐标系)(x ...

  6. Qt ------ QTabWidget

    下图: 1.长方形的 objectName 可写可不写,不写就作用于所有 QTabWidget:椭圆形的 QTabWidget#tabWidget 要么四个都要写,要么四个都不写 2.下图的 CSS ...

  7. 「Python」35个知识点

    No.1 一切皆对象 众所周知,Java中强调“一切皆对象”,但是Python中的面向对象比Java更加彻底,因为Python中的类(class)也是对象,函数(function)也是对象,而且Pyt ...

  8. 「Python」5个开源项目

    1-OpenAI universe Universe是一个能在世界上所有的游戏.网站和其他应用中,衡量和训练AI通用智能的软件平台. Universe,AI代理通过称为虚拟网络计算或VNC发送模拟的鼠 ...

  9. [DeeplearningAI笔记]序列模型1.3-1.4循环神经网络原理与反向传播公式

    5.1循环序列模型 觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.3循环神经网络模型 为什么不使用标准的神经网络 假如将九个单词组成的序列作为输入,通过普通的神经网网络输出输出序列, 在 ...

  10. HDFS默认副本数为什么是3

    转载自: https://www.cnblogs.com/bugchecker/p/why_three_replications_for_HDFS_in_engineer.html HDFS采用一种称 ...