【Search in Rotated Sorted Array II 】cpp
题目:
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.
代码:
class Solution {
public:
bool search(vector<int>& nums, int target) {
nums.erase(std::unique(nums.begin(), nums.end()),nums.end());
int begin=, end=nums.size()-;
while ( begin<=end )
{
int mid = (begin+end)/;
if ( nums[mid]==target ) return true;
// first half sorted
if ( nums[begin]<=nums[mid] )
{
if ( target>nums[mid] )
{
begin = mid+;
}
else
{
if ( target>=nums[begin] )
{
end = mid-;
}
else
{
begin = mid+;
}
}
continue;
}
// second half sorted
if ( nums[mid]<nums[end] )
{
if ( target<nums[mid])
{
end = mid-;
}
else
{
if ( target<=nums[end])
{
begin = mid+;
}
else
{
end = mid-;
}
}
}
}
return false;
}
};
tips:
通过这题熟悉了stl将vector去重的方法。
采用了偷懒的做法:
1. 先利用stl的unqiue把数组去重
2. 再按照Search in Rotated Sorted Array这题的方法进行二分查找。
【Search in Rotated Sorted Array II 】cpp的更多相关文章
- leetcode 【 Search in Rotated Sorted Array II 】python 实现
题目: 与上一道题几乎相同:不同之处在于array中允许有重复元素:但题目要求也简单了,只要返回true or false http://www.cnblogs.com/xbf9xbf/p/42545 ...
- 【Remove Duplicates from Sorted Array II】cpp
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 【leetcode】Search in Rotated Sorted Array II
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 【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 ...
- 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 解题报告& ...
随机推荐
- webpack整体了解
一.下载 新建一个文件夹,在cmd中npm init->npm install->npm install webpack --save-dev 下载完成之后,新建一个webpack.con ...
- leetcode: 字符串
1. palindrome-partitioning Given a string s, partition s such that every substring of the partition ...
- 分治——sqtx
题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- java——二叉树面试题
import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util ...
- 【转载】#349 - The Difference Between Virtual and Non-Virtual Methods
In C#, virtual methods support polymorphism, by using a combination of the virtual and override keyw ...
- UESTC 757 棋盘
虽然是水题,但是还是很interesting的.(大概就是我最晚出这个题了... 博弈感觉就是靠yy能力啊.这题是对称性. 最后的必败态是白色格子对称的,一旦对称形成,对手怎么选,跟随就好,对手无法摆 ...
- Cydia Tweak--Cydia Substrate
http://www.jianshu.com/p/8982e9670fc6 Cydia Substrate.MobileHooker MSHookMessageEx MSHookFunction Mo ...
- OpenGL进阶演示样例1——动态画线(虚线、实线、颜色、速度等)
用OpenGL动态绘制线段.事实上非常easy,但到如今为止.网上可參考资料并不多. 于是亲自己主动手写一个函数,方便动态绘制线段.代码例如以下: #include<GL/glu ...
- Poj(1182),种类并查集
题目链接:http://poj.org/problem?id=1182 再次熟练种类并查集,又积累点经验,和技巧,rank 0 2 1 先计算father[x] ,再更新rank[x]; #inclu ...
- 【转】android gravity属性 和 weight属性
有点忘记这两个属性了,复习一下. 来看这个布局文件 <?xml version="1.0" encoding="utf-8"?> <Linea ...