[Leetcode] search in rotated sorted array ii 搜索旋转有序数组
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.
题意:查询给定目标值是否在数组中,是search in rotated sorted array的扩展。
思路:因为是上一题的扩展,所以很正常的想到,上题的思路,在本题是否合适。结果发现不能强行的套上去。如:1,1,3,1,target=3,若是按照原来的解法,则应该是左边有序,然而不行;若是if条件判断中间等号,为右边有序,也显然不对。参考了Grandyang的博客才知道,只需在判断上加上判断中间值是否等于最右端值,若是,hi向左移动一个,直到不相等。这样就可以继续保持上题的解法了,具体代码如下:
class Solution {
public:
bool search(int A[], int n, int target)
{
if(n==) return false;
int lo=,hi=n-;
while(lo<=hi)
{
int mid=(lo+hi)/;
if(A[mid]==target)
return true;
else if(A[mid]<A[hi])
{
if(A[mid]<target&&A[hi]>=target)
lo=mid+;
else
hi=mid-;
}
else if(A[mid]>A[hi])
{
if(A[lo]<=target&&A[mid]>target)
hi=mid-;
else
lo=mid+;
}
else //重点理解
hi--;
}
return false;
}
};
[Leetcode] search in rotated sorted array ii 搜索旋转有序数组的更多相关文章
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 081 Search in Rotated Sorted Array II 搜索旋转排序数组 ||
这是 “搜索旋转排序数组”问题的跟进:如果数组元素允许重复,怎么办?这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?假设按照升序排序的数组在预先未知的某个关键点上旋转.(例如, 0 1 2 4 ...
- [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 ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- [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. ...
- LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)
题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description 姊妹篇:http://www. ...
- LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- hugepages_settings.sh
#!/bin/bash## hugepages_settings.sh## Linux bash script to compute values for the# recommended HugeP ...
- Qt-QML-C++交互实现文件IO系统-后继-具体文件IO的实现
在上一篇文章中,我大致将这个QML中的文件IO类搭出了大致的框架,那么,今天抽时间写了一点文件的读写,这里我使用的文件流来读写文件. 文件结构如图 在QML中调用这个类了,就见简单的读取了一个JSON ...
- hello-jni Android.mk文件简析
#删除旧变量 LOCAL_PATH := $(call my-dir) #返回当前目录 include $(CLEAR_VARS) #删除旧变量 #设置新变量 LOCAL_MODULE := hell ...
- Python中一些糟糕的语法!你遇到过吗?还知道那些?
Python是一门语法优雅,功能强大,开发效率高,应用领域广泛的解释性语言. 其有非常多的优点,但是也并不是完美的,除了大家都知道的执行速度不够快,Python2和Python3的兼容问题,以及GIL ...
- Navicat和DBeaver的查询快捷键
1.Navicat for MySQL(连接MySQL数据库的工具) ctrl + r 执行查询页中所有的sql语句 ctrl + shift + r 只运行选中的sql语句 2.DBeaver(支持 ...
- python3 SQLAlchemy模块使用
更详细的操作介绍:https://www.imooc.com/article/22343 定义: SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对 ...
- Paper Reading - Learning like a Child: Fast Novel Visual Concept Learning from Sentence Descriptions of Images ( ICCV 2015 )
Link of the Paper: https://arxiv.org/pdf/1504.06692.pdf Innovations: The authors propose the Novel V ...
- 关于mysql开元数据库的几个随想
现在已经是凌晨了,昨天晚上写了我人生中的第一篇笔记,觉得没什么可写的,写了一个多小时都没写出什么,现在突然想写点东西了,这是一个比较有趣的问题,前两个月换了新工作,记得当初面试这份工作的时候面试到第三 ...
- 如何让thinkpad X1C 用U盘 安装上专业版win10
1 BIOS内置了文件 会导致win10 iso默认装家庭版 2 给iso 的resouse 目录中增加文件ei.cfg 3 内容如下 [EditionID]Professional[Channel] ...
- Thunder团队第三周 - Scrum会议1
Scrum会议1 小组名称:Thunder 项目名称:i阅app Scrum Master:王航 工作照片: 杨梓瑞在拍照,所以不在照片中. 参会成员: 王航(Master):http://www.c ...