【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 解题报告& ...
随机推荐
- ABAP Netweaver和Hybris Enterprise Commerce Platform的登录认证
ABAP Netweaver 在我的博客Learn more detail about Standard logon procedure里有详细介绍. Hybris ECP Hybris Admini ...
- Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning
backtracking and invariant during generating the parathese righjt > left (open bracket and cloas ...
- python 面向对象(一)--类(class)和实例(Instance)
面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可 ...
- frcnn_train_data_param的distort_param实现
frcnn_train_data_param frcnn_train_data_param { source: "./data/train_list.txt" root_folde ...
- R 语言学习日志 1
1. CSV文件的的读取与写出 2. 数据集筛选 3. 简单随机抽样 sample函数 正文: 1. CSV文件的的读取与写出 文件读取: df2 <- read.table(" ...
- LinkedHashMap使用心得
基本概念:https://www.cnblogs.com/xiaoxi/p/6170590.html 遇到问题: 1.构造方法 public static LinkedHashMap<Strin ...
- 在jdbc基础上进阶一小步的C3p0 连接池(DBCP 不能读xml配置文件,已淘汰) 和DBUtils 中两个主要类QueryRunner和ResultSetHandler的使用
首先看C3p0这个连接池,最大优势可以自动读取默认的配置文件 <?xml version="1.0" encoding="UTF-8"?> < ...
- XML DTD约束 对xml文件的crud的查询Read Retrieve操作 xml递归遍历
本地的dtd文档 xml中引入dtd文档 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE 书 ...
- c++ 11 线程池的简单封装
#include <condition_variable> #include <queue> #include <thread> #include <vect ...
- webpack4 单独抽离打包 css 的新实现
webpack4 单独抽离打包 css 的新实现 前言 之前我们使用的打包 css 无非两种方式:① 将 css 代码打包进 入口 js 文件中:② 使用第三方插件(extract-text-webp ...