【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": 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.
解法一:暴力解法,直接使用algorithm库中的求最小元素函数
需要遍历整个vector
class Solution {
public:
int findMin(vector<int> &num) {
if(num.empty())
return ;
vector<int>::iterator iter = min_element(num.begin(), num.end());
return *iter;
}
};

解法二:利用sorted这个信息。如果平移过,则会出现一个gap,也就是从最大元素到最小元素的跳转。如果没有跳转,则说明没有平移。
比上个解法可以省掉不少时间,平均情况下不用遍历vector了。
class Solution {
public:
int findMin(vector<int> &num) {
if(num.empty())
return ;
else if(num.size() == )
return num[];
else
{
for(vector<int>::size_type st = ; st < num.size(); st ++)
{
if(num[st-] > num[st])
return num[st];
}
return num[];
}
}
};

解法三:二分查找
与Find Minimum in Rotated Sorted Array对照看,
一共有两处修改。
1、在无重复元素时,首尾元素相等代表指向同一个位置,因此程序直接返回即可。
然而当存在重复元素时,该条件并不能表示指向同一个位置,因此
nums[low] > nums[high]
改为
nums[low] >= nums[high]
2、在无重复元素时,中间元素与首元素相等,表示一共只有两个元素,low与high各指向一个。
由于while循环中限制的大小关系,因此返回nums[high]即为最小值。
然而当存在重复元素时,该条件并不能表示一共只有low和high指向的两个元素,
而是说明low指向的元素重复了,因此删除其一,low ++即可。
class Solution {
public:
int findMin(vector<int>& nums) {
if(nums.empty())
return ;
if(nums.size() == )
return nums[];
int n = nums.size();
int low = ;
int high = n-;
while(low < high && nums[low] >= nums[high])
{
int mid = low + (high-low)/;
if(nums[mid] < nums[low]) // mid is in second part
high = mid;
else if(nums[mid] == nums[low])
low ++;
else
low = mid+;
}
return nums[low];
}
};

【LeetCode】154. Find Minimum in Rotated Sorted Array II (3 solutions)的更多相关文章
- 【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
Find Minimum in Rotated Sorted Array II Suppose an array sorted in ascending order is rotated at som ...
- 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)
[LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】153. Find Minimum in Rotated Sorted Array (3 solutions)
Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you ...
- LeetCode OJ 154. Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- 【leetcode】153. Find Minimum in Rotated Sorted Array
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example ...
- 【原创】leetCodeOj --- Find Minimum in Rotated Sorted Array II 解题报告
题目地址: https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目内容: Suppose a sort ...
- 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 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法
Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...
随机推荐
- 2012年及之后的ImageNet比赛的冠军、亚军和季军ImageNet winners after 2012
2012 0.15 - Supervision (AlexNet) - ~ 60954656 params 0.26 - ISI (ensemble of features) 0.27 - LEAR ...
- 5.synchronized锁重入
package demo1; /** * synchronized锁重入 * Created by liudan on 2017/6/5. */ public class MyThread5_sync ...
- 解决Ubuntu下的Eclipse打开Windows编写的java代码的中文乱码
其实所有的中文乱码 问题都是编码不同所导致的.这里要想让eclipse能正常显示出汉字,就要修改它的字符编码 步骤如下: 1 ,点击菜单栏中的Window(窗口),选择Preferences(首选项) ...
- Informatica 常用组件Lookup之四 查找组件
在映射中配置查找转换时,请定义以下组件: 查找源 端口 属性 条件 元数据扩展 查找源 您可以使用平面文件或关系表作为查找源.创建查找转换时,您可以从以下位置导入查找源: 资料库中的任 ...
- linux下永久添加静态路由
在linux下永久添加静态路由有两种方法: 添加路由的命令: 1,route add route add -net 192.56.76.0 netmask 255.255.255.0 dev eth0 ...
- 一个巧妙的方法实现elementUI的table的行选中
问题背景:点击上面的框,选中下面对象的行数据 刚开始考虑使用的是table的事件:toggleRowSelection,但是发现一个奇怪的现象 <div v-if="orderData ...
- [转]How to query posts filtered by custom field values
Description It is often necessary to query the database for a list of posts based on a custom field ...
- 【python】用正则表达式进行文字局部替换
比如有个字符串http://www.55188.com/thread-8306254-2-3.html,需要把8306254后面的2替换成其它数字,其它保持不变,该如何办呢?请看代码: import ...
- 使用(function() {}).call(this);包裹代码有什么好处,什么时候应该这样做?
转自:http://segmentfault.com/q/1010000002519489 1.严格模式下函数调用的 this 并不会默认成为全局对象. 使用 func.call(this) 确保函数 ...
- Web前端开发资源集锦
前端开发已经成为当前炙手可热的技术之一.本周我们除了给大家带技术相关资讯,还有一些技术人员常用的网站.希望大家不要错过我们本周的内容.原文来自:极客标签 为神马说写程序是很艰难的 程序员 做一名优秀程 ...