LeetCode(154) Find Minimum in Rotated Sorted Array II
题目
Follow up for “Find Minimum in Rotated Sorted Array”:
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
Find the minimum element.
The array may contain duplicates.
Subscribe to see which companies asked this question
分析
在含有重复元素的旋转有序序列中查找最小元素。
与上一题类似,LeetCode(153) Find Minimum in Rotated Sorted Array 153题中的旋转有序数组不包含重复元素,而此题允许重复元素,增加了一点难度。
我想题目重点考察的还是沿用二分查找的方法解决,思路参考。
AC代码
class Solution {
public:
//方法一:使用stl库函数
int findMin1(vector<int>& nums) {
if (nums.empty())
return 0;
vector<int>::iterator iter = min_element(nums.begin(), nums.end());
return *iter;
}
//方法二:整个数列除一处为最大值到最小值的跳转外,为两部分的递增
int findMin2(vector<int>& nums)
{
if (nums.empty())
return 0;
if (nums.size() == 1)
return nums[0];
for (size_t i = 1; i < nums.size(); ++i)
{
if (nums[i - 1] > nums[i])
return nums[i];
}//for
//没有找到跳转元素,则序列无旋转
return nums[0];
}
int findMin(vector<int> &nums)
{
if (nums.empty())
return 0;
else if (nums.size() == 1)
return nums[0];
else{
int lhs = 0, rhs = nums.size() - 1;
while (lhs < rhs && nums[lhs] >= nums[rhs])
{
int mid = (lhs + rhs) / 2;
if (nums[lhs] > nums[mid])
rhs = mid;
else if (nums[lhs] == nums[mid])
++lhs;
else
lhs = mid + 1;
}//while
return nums[lhs];
}
}
};
LeetCode(154) Find Minimum in Rotated Sorted Array II的更多相关文章
- LeetCode(153) Find Minimum in Rotated Sorted Array
题目 Total Accepted: 65121 Total Submissions: 190974 Difficulty: Medium Suppose a sorted array is rota ...
- Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)
Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II) 假设按照升序排序的数组在预先未知的某个点上进 ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- LeetCode 154.Find Minimum in Rotated Sorted Array II(H)(P)
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
- 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 ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II (3 solutions)
Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...
- 【刷题-LeetCode】154 Find Minimum in Rotated Sorted Array II
Find Minimum in Rotated Sorted Array II Suppose an array sorted in ascending order is rotated at som ...
- LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法
Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...
- [OJ] Find Minimum in Rotated Sorted Array II
LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...
随机推荐
- C. Epidemic in Monstropolis
http://codeforces.com/contest/733/problem/C 一道很恶心的模拟题. 注意到如果能凑成b[1],那么a的前缀和一定是有一个满足是b[1]的,因为,如果跳过了一些 ...
- spirngmvc整合mybatis
一.建立一张简单的User表 CREATE TABLE `users` (`id` int(20) NOT NULL AUTO_INCREMENT,`name` varchar(20) NOT NUL ...
- C#实现程序单例日志输出
对于一个完整的程序系统,一个日志记录是必不可少的.可以用它来记录程序在运行过程中的运行状态和报错信息.比如,那些不想通过弹框提示的错误,程序执行过程中捕获的异常等. 首先,在你的解决方案中,适当的目录 ...
- 关于 SQL Server Reporting Services 匿名登录的解决方案
每次访问报表都需要windows验证,这样的报表给客户确实很说不过去. SSRS 可以匿名登录的设定步骤: 环境: 开发工具:SQL Server Business Intelligence Deve ...
- (译)Minimal Shader(最小的着色器)
(原文:https://en.wikibooks.org/wiki/Cg_Programming/Unity/Minimal_Shader) This tutorial covers the basi ...
- Layer:如何调用layer.open打开的的iframe窗口中的JS?
layer.open({type: 2,content: 'test/iframe.html',success: function(layero, index){ var body = layer.g ...
- SQL SEVER数据库重建索引的方法
一.查询思路 1.想要判断数据库查询缓慢的问题,可以使用如下语句,可以列出查询语句的平均时间,总时间,所用的CPU时间等信息 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- FZU 2204 7
题意: n个有标号的球围成一个圈.每个球有两种颜色可以选择黑或白染色.问有多少种方案使得没有出现连续白球7个或连续黑球7个? 思路: 如果出现连续的8,9...个球同色,那么也必定含有7个同色.需要统 ...
- codeforces Gym 100286J Javanese Cryptoanalysis (二染色)
每一单词相邻两个字母,不能同时为元音或者辅音... 各种姿势都可以过:7个for,dp,黑白染色,dfs,并查集.... 最主要的思路就是相邻字母连边,把元音和辅音看成两个集合,那么有连边的两个字母一 ...
- python_87_shelve模块
'shelve模块是一个简单的key,value将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式(只支持pickle)' #序列化,将数据写入文件 import ...