Search in Rotated Sorted Array II
Question:
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.
Solution:
class Solution {
public:
bool search(vector<int>& nums, int target)
{
int left=;
int right=nums.size()-;
if(left==right && nums[left]==target)
return true;
while(left!=right)
{
int mid=(left+right)/;
if(nums[mid]==target) return true;
if(nums[left]<nums[mid])
{
if(nums[left]<=target && target<=nums[mid])
right=mid;
else
left=mid+;
}
else if(nums[left]>nums[mid])
{
if(nums[mid]<=target && target<=nums[right])
left=mid+;
else
right=mid;
}
else
left++;
if(left==right && nums[left]==target)
return true;
}
return false;
}
};
Search in Rotated Sorted Array II的更多相关文章
- 【leetcode】Search in Rotated Sorted Array II
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...
- 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II
33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【leetcode】Search in Rotated Sorted Array II(middle)☆
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- 几点基于Web日志的Webshell检测思路
http://www.open-open.com/lib/view/open1456751673359.html
- 二叉查找树的查找、插入和删除 - Java实现
http://www.cnblogs.com/yangecnu/p/Introduce-Binary-Search-Tree.html 作者: yangecnu(yangecnu's Blog on ...
- JS中的this好神奇,都把我弄晕了
一.this的常见判断: 1.函数预编译过程 this —> window 2.全局作用域里 this —> window 3.call/apply 可以改变函数运行时this指向 4.o ...
- linq 常用语句
自己练习的 switch (productDataAnalysisQuery.DataType) { : var data = (from hp in GPEcontext.hbl_product j ...
- 集成 Tomcat 插件到 Eclipse 的过程
Java代码: . 下载 Tomcat Tomcat6,下载地址:http://tomcat.apache.org/download-60.cgi,选择绿色版的 zip 进行下载(目前最新的 Tomc ...
- TCP协议RST:RST介绍、什么时候发送RST包
TCP协议RST:RST介绍.什么时候发送RST包 RST标示复位.用来异常的关闭连接. 1. 发送RST包关闭连接时,不必等缓冲区的包都发出去,直接就丢弃缓冲区中的包,发送RST. 2. 而接收端收 ...
- MDK4.6和J-LINK调试出现问题,软件自动关闭,在网上收集整理的解决办法
MDK4.6配J-LINK调试时提示升级,升级完成后,弹出下图提示框后,软件自动退出. 提示原因:由于MDK4.6能识别山寨JLINK导致.网络牛人分析如下: 今天将Keil MDK升级到了V4.54 ...
- iOS 苹果真机鉴定
iPhone 4S(GSM) 16GB 黑色序列号:DX4KN69EDTC0设备名称:iPhone 4S容 量:16GB颜 色:黑色类 型:iPhone4,1代 号:n94ap型 号:MD235激活状 ...
- jboss内存管理
jboss内存查看 1. 用浏览器打开网址:http://IP:port/jmx-console/ 2. 找到 jboss.system 一节,或者在 filter 文本框中输入 jboss.syst ...
- 《mysql悲观锁总结和实践》-悲观锁
最近学习了一下数据库的悲观锁和乐观锁,根据自己的理解和网上参考资料总结如下: 悲观锁介绍(百科): 悲观锁,正如其名,它指的是对数据被外界(包括本系统当前的其他事务,以及来自外部系统的事务处理)修改持 ...