【刷题-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 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.
Example 1:
Input: [1,3,5]
Output: 1
Example 2:
Input: [2,2,2,0,1]
Output: 0
Note:
- This is a follow up problem to Find Minimum in Rotated Sorted Array.
- Would allow duplicates affect the run-time complexity? How and why?
解 三种情况
class Solution {
public:
int findMin(vector<int>& nums) {
if(nums.size() == 1)return nums[0];
int l = 0, r = nums.size()-1, mid;
if(nums[r] > nums[l])return nums[l];
while(l < r){
mid = (l+r)/2;
if(nums[mid] > nums[r])l = mid+1;
else if(nums[mid] < nums[r])r = mid;
else r = r - 1;
}
return nums[l];
}
};
【刷题-LeetCode】154 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 寻找旋转有序数组的最小值之二
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i. ...
- 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. Find Minimum in Rotated Sorted Array II --------- java
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- LeetCode 154. Find Minimum in Rotated Sorted Array II寻找旋转排序数组中的最小值 II (C++)
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
- [LeetCode#154]Find Minimum in Rotated Sorted Array II
The question: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are ...
- 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】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 ...
随机推荐
- Python sys模块 os模块、OS.open() | open() | OS._exit() | sys.exit() | exit()
sys模块:负责程序和Python交互. sys常用方法:=========================== sys.stdout.write('please:')val = sys.stdin ...
- java 常用类库:时间类LocalDate;LocalTime;LocalDateTime;Calendar 类;Date ;
LocalDate类 LocalDate类代表不带时区的日期,列入2020-12-20.该类提供了静态的now()方法来获取当前的日期.这个类是线程安全的. LocalTime类 代表不带时区的时间, ...
- RIP2与OSPFv2 动态路由协议区别
OSPF五种报文解析 Hello:招呼信息 Route-ID:换回口地址/活动的物理接口最大值 Hello作用: 1. 发现邻居 2. 对一些数据的协商 3. 保持邻居的Keeplive状态.选举DR ...
- C++11 新特性:enable_shared_from_this
enable_shared_from_this是一个模板类,定义于头文件<memory>,其原型为:template< class T > class enable_share ...
- js判断是电脑(pc)访问还是手机(mobile)访问
<script> if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Backerry|WebOS|Symb ...
- 【LeetCode】1133. Largest Unique Number 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 桶排序 日期 题目地址:https://leetcod ...
- 【LeetCode】730. Count Different Palindromic Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化搜索 动态规划 日期 题目地址:https:/ ...
- Redis的各种实现(Jedis、Redisson、Lettuce)
1.1.Jedis 是Redis的Java实现的客户端.支持基本的数据类型如:String.Hash.List.Set.Sorted Set. 特点:使用阻塞的I/O,方法调用同步,程序流需要等到so ...
- 替代瑞昱RTD2166|pin对pin替代RTD2166|CS5202芯片
替代瑞昱RTD2166,pin对pin替代RTD2166,外围器件少,设计版框尺寸小,整套方案成本BOM更低. 一. CS5202功能概述 CS5202是一款DP端口到VGA转换器,它结合了DP输入 ...
- dart系列之:dart代码最佳实践
目录 简介 命名规则 import中的顺序 格式化 总结 简介 每种语言都有自己的代码风格,这种代码风格是跟语言特性息息相关的.如果在编码的过程中遵循这种统一的编码规则,会给我们的业务带来非常多的便利 ...