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 ...
随机推荐
- sql 语句的各种连接
数据表: 1.两种连接表现方式一样 其中 where 条件语句可以省略,当时join 的on 语句不可省略 2.左连接,右连接 左连接:返回左表的所有数据,并根据条件返回左右表的连接结果,如果未匹配到 ...
- (转)Android之ListView原理学习与优化总结
转自: http://jishu.zol.com.cn/12893.html 在整理前几篇文章的时候有朋友提出写一下ListView的性能优化方面的东西,这个问题也是小马在面试过程中被别人问到的….. ...
- return x>y?x:y ?:啥意思?
? :是一个三目运算符,先判断‘?’前面的,若为真,执行‘?’后面语句,else,执行‘:’后面语句! return (x>y?x:y) 即if(x>y) 执行xelse执行y
- Android View体系
- JAVA类型信息——Class对象
JAVA类型信息——Class对象 一.RTTI概要 1.类型信息RTTI :即对象和类的信息,例如类的名字.继承的基类.实现的接口等. 2.类型信息的作用:程序员可以在程序运行时发现和使用类型信息. ...
- UVA 10780 Again Prime? No Time. 分解质因子
The problem statement is very easy. Given a number n you have to determine the largest power of m,no ...
- JQuery的第一天实战学习
1.按照下面的工程来建: 2.新建UserVerify.html文件: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitiona ...
- sublime3可用key
—– BEGIN LICENSE —–Nicolas HennionSingle User LicenseEA7E-8660758A01AA83 1D668D24 4484AEBC 3B04512C8 ...
- iOS开发--appstore应用上架
除了企业级的应用,一般一个应用开发完成后,都会上架App Store.其实上架流程并不繁琐,麻烦的是要耗时等待审核,如果被拒,修改后又需要等待.被拒的原因很多(真的很多…),比如程序有崩溃,适配没做好 ...
- Python十分钟学会
初试牛刀 假设你希望学习Python这门语言,却苦于找不到一个简短而全面的入门教程.那么本教程将花费十分钟的时间带你走入Python的大门.本文的内容介于教程(Toturial)和速查手册(Cheat ...