[LeetCode] 153. Find Minimum in Rotated Sorted Array_Medium tag: Binary Search
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.
You may assume no duplicate exists in the array.
Example 1:
Input: [3,4,5,1,2]
Output: 1
Example 2:
Input: [4,5,6,7,0,1,2]
Output: 0
这个题目思路就是找到first number s.t <= nums[-1].利用Binary Search, 就是注意是左移还是右移,最好画图判断下。
T: O(lgn)
Code
class Solution:
def minRotatedArray(self, nums):
l, r, target = 0, len(nums) -1, nums[-1]
if not nums: return -1
while l + 1 < r:
mid = l + (r - l)//2
if nums[mid] <= target:
r = mid
else:
l = mid
return min(nums[l], nums[r])
[LeetCode] 153. Find Minimum in Rotated Sorted Array_Medium tag: Binary Search的更多相关文章
- [LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- 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] 153. Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- Java for LeetCode 153 Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- LeetCode 153. Find Minimum in Rotated Sorted Array (在旋转有序数组中找到最小值)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- LeetCode 153.Find Minimum in Rotated Sorted Array(M)(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
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- leetcode 153. Find Minimum in Rotated Sorted Array --------- java
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- Leetcode 153. Find Minimum in Rotated Sorted Array -- 二分查找的变种
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
随机推荐
- rxjs 常用的静态操作符
操作符文档 API create const { Observable } = require('rxjs'); // 创建 Observables var observable = Observab ...
- MSI/MSI-X Capability结构 (转)
http://blog.sina.com.cn/s/blog_6472c4cc0102dskj.html
- okvis代码解读11
https://blog.csdn.net/datase/article/details/78586854 https://www.cnblogs.com/JingeTU/p/8540426.html ...
- HDU 4699 - Editor - [对顶栈]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4699 Problem Description Sample Input8I 2I -1I 1Q 3LD ...
- [No0000159]C# 7 中的模范和实践
关键点 遵循 .NET Framework 设计指南,时至今日,仍像十年前首次出版一样适用. API 设计至关重要,设计不当的API大大增加错误,同时降低可重用性. 始终保持"成功之道&qu ...
- [No0000E0]批量打开当前路径下的文件
for /r %i in ( *) do start %i
- Mysql 常用调优命令总结
在工作中,经常会碰到Mysql性能问题,本文记录了调优时会用到的一些命令: 1. show full processlist\G: 显示Mysql服务器正在执行的线程.有root权限的用户可以显示所有 ...
- angular 表单元素的使用总结
工作中form表单元素最常用的是input,问题没有太多,现在总结下select ,radio组,checkbox的使用 1 select 常用的使用方式,如下 var Cityis = [{id:0 ...
- java之String类在堆栈存储机制
String类是一个比较特殊的类,最主要的体现是它有多种创建形式,例如,String a ="abc";Sting a=new("abc");表面上看得到的结果 ...
- liteide 去除go程序cmd窗口
http://blog.csdn.net/aqtata/article/details/53389261