LeetCode——Find Minimum in Rotated Sorted Array II
Question
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 an array sorted in ascending order 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.
Solution
抓住数组的特点。
Code
class Solution {
public:
int findMin(vector<int>& nums) {
int size = nums.size();
if (size == 1)
return nums[0];
int low = 0;
int high = size - 1;
while (low < high) {
// 没用递归,所以注意求中间节点的方法
int mid = low + (high - low) / 2;
// 如果更小,那么会比右边的都小
if (nums[mid] < nums[high])
high = mid;
// 如果更大,那最小的肯定在右边
else if (nums[mid] > nums[high])
low = mid + 1;
// 如果相等,不知道最大值在左边还是在右边
else
high--;
}
return nums[low];
}
};
LeetCode——Find Minimum in Rotated Sorted Array II的更多相关文章
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- LeetCode Find Minimum in Rotated Sorted Array II
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...
- [leetcode]Find Minimum in Rotated Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 解题思路:这道题和上一道题的区别是,数组中 ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- 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 寻找旋转有序数组的最小值 II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 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 ...
随机推荐
- Python并行编程(四):线程同步之RLock
1.基本概念 如果想让只有拿到锁的线程才能释放该锁,那么应该使用RLock()对象.当需要在类外面保证线程安全,又要在类内使用同样方法的时候RLock()就很使用. RLock叫做Reentrant ...
- SDUT3141:Count(哈希)好题
题目:传送门 题目描述 You are given an integer array s[] and are asked to count how many positions a, b, c and ...
- CCoolBar 的替代方案 CDockablePane。
(阅读受众需有一定MFC知识储备.) (技术支持:http://www.cnblogs.com/shuhaoc/archive/2011/06/26/cdockableform.html) 在以往很多 ...
- Windows 和 Linux 的文件名
Windows中文件名是区分大小写的,而Linux不区分. 在开发中,发现在Windows可以执行通过,在Linux可能由于文件名不一致而失败.
- Mock Server 之 moco-runner 使用指南二
文章出处http://blog.csdn.net/crisschan/article/details/53335234 moco-runner 安装配置 1. 下载jar https://repo1. ...
- 实现:左边为菜单导航,当一个菜单中包含多个Tabs,并且不同的Tab要根据权限的不同显示。
1.前台代码 //当点击左侧菜单时,将访问Controller中的Home方法,这样就会根据用户权限的不同,通过后台的判断来决定显示的页面<li class="@(ViewBag.Se ...
- Linux系统——shell脚本
shell脚本编程 作用:通过命令行解析的方式,自动执行设定好的程序或命令代码.(若将脚本挂到定时任务中,就会自动在非工作时间里自动触发执行程序) Shell脚本文件以“.sh”结尾 规范的Shell ...
- mac shell
查看所有shell:cat /etc/shells 查看当前正在使用的shell:echo $SHELL 切换shell:chsh -s /bin/zsh
- 基于bootstrap metronic-responsive-admin-dashboard-template 开发管理后台
简单介绍 我们这个系统是基于bootstrap metronic-responsive-admin-dashboard-template 这个模板开发的.版本用的是metronic_v4.5.2 效果 ...
- @Transactional(rollbackFor=Exception.class)的使用
转载: java阿里巴巴规范提示:方法[edit]需要在Transactional注解指定rollbackFor或者在方法中显示的rollback. 先来看看异常的分类 error是一定会回滚的 这里 ...