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 ...
随机推荐
- js Ajax 跨域请求
一.使用jsonp的方式(只支持get请求) 二.使用cors的方式(支持HTTP的大部分请求方式) 三.apache的转发(修改服务器配置) 没有试验,暂时不详细写!
- shell基础知识总结
1. shell 对于一台计算机而言,其硬件受系统内核的控制,使用者想要控制计算机,就必须有与系统内核进行通讯的手段.而shell就是使用者与计算机进行通讯的手段之一.从命名上看,shell其实是相对 ...
- 系列:每日一linux命令(转)
原文:http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html 一. 文件目录操作命令: 1.每天一个linux命令(1):ls命令 2 ...
- 【IPC进程间通讯之中的一个】邮槽MailSlot
IPC进程间通信+邮槽MailSlot IPC(Inter-Process Communication.进程间通信). 现代计算机採用虚拟内存机制,为进程提 ...
- 007-mac快捷键
锁屏:Ctrl + Command + Q touch-bar:方法:“系统偏好设置”>“键盘”>“自定Control Strip…”,将“锁定屏幕”图标拖拽到Touch Bar上即可.] ...
- JAVA与ACM
这两天学了一下JAVA的语法,还没有学习后面的核心地方,突然间觉得JAVA这门语言很棒,我要在接下来的时间系统的学习一下.就这么愉快地决定了. Java对于大数计算这方面的优势很大.最重要的是代码量小 ...
- [acm/icpc2016ChinaFinal][CodeforcesGym101194] Mr. Panda and Fantastic Beasts
地址:http://codeforces.com/gym/101194 题目:略 思路: 这题做法挺多的,可以sam也可以后缀数组,我用sam做的. 1.我自己yy的思路(瞎bb的) 把第一个串建立s ...
- iOS 学习 RESTful 中 Http 的幂等性
一. RESTful RESTful (Representational State Transfer) 是一种常用流行的软件架构,设计风格或协议标准.提供了一组设计风格和约束条件.主要用于客户端和 ...
- [2010-12-28 20:39]ActionScript3.0中用要的公式
一.质点的运动(1)——直线运动 1)匀变速直线运动 1.平均速度V平=s/t(定义式) 2.有用推论Vt2-Vo2=2as 3.中间时刻速度Vt/2=V平=(Vt+Vo)/2 4.末速度Vt=Vo+ ...
- python3_ftp多线程上传图片
项目中研发人员自己写了ftp服务,没有标准ftp中的列表,准备用jmeter对ftp压力测试时发现jmeter要验证列表(如果有同学用jmeter测试过类似的分享一下方法谢谢了),没办法只能用pyth ...