LeetCode——Find Minimum in Rotated Sorted Array
Description:
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.
You may assume no duplicate exists in the array.
随机找一个枢纽,旋转有序数组,找出最小元素。
O(n)解法,线性查找。1ms
public class Solution {
public int findMin(int[] nums) {
int min = nums[0];
for(int i=1; i<nums.length; i++) {
if(min > nums[i])
min = nums[i];
}
return min;
}
}
O(logn)解法,二分。0ms
public class Solution {
public int findMin(int[] nums) {
int n = nums.length - 1;
int left = 0, right = nums.length - 1;
while(left <= right) {
int mid = left + (right - left) / 2;
if(nums[mid] > nums[n]) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return nums[left];
}
}
LeetCode——Find Minimum in Rotated Sorted Array的更多相关文章
- Leetcode Find Minimum in Rotated Sorted Array 题解
Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...
- [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 寻找旋转有序数组的最小值
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- 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
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...
- Leetcode | Find Minimum in Rotated Sorted Array I && 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——Find Minimum in Rotated Sorted Array II
Question Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allo ...
- Leetcode Find Minimum in Rotated Sorted Array I and 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] 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 ...
随机推荐
- python 提示 AttributeError: module 'json' has no attribute 'dumps'
最近在学习python解析json的时候遇到问题, 提示 AttributeError: module 'json' has no attribute 'dumps' (模块没有dumps属性的意思) ...
- spring复习 -day1:Spring简介 、 Spring容器 、 Spring IOC
Spring创建对象方法 创建对象过程: 第一步:添加SpringIOC环境 (1)在WebRoot/WEB-INT/lib文件夹下,引入SpringIOC配置环境的jar包 如图: (2)在sr ...
- 选择列表中的列……无效,因为该列没有包含在聚合函数或 GROUP BY 子句中
今天用SQL Server尝试实现一个SQL语句的时候,报了如标题所示的错误,通过在百度里面搜索,并亲自动手实现,终于发现问题所在,现在把它记录下来. 语句如下: select [OrderID],[ ...
- mongodb自动关闭:页面太小,无法完成操作
解决方法: 增大虚拟内存
- (转)RTP协议全解(H264码流和PS流)
写在前面:RTP的解析,网上找了很多资料,但是都不全,所以我力图整理出一个比较全面的解析, 其中借鉴了很多文章,我都列在了文章最后,在此表示感谢. 互联网的发展离不开大家的无私奉献,我决定从我做起,希 ...
- linux -- chown修改文件拥有者和所在组
chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...
- linux gzip 命令详解
减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用. 语法:gzip ...
- 一个不错的在线的js调试器
一个不错的在线的js调试器,可见即可得: http://jsbin.com/
- UART通信协议
第一部分: UART使用的是 异步,串行通信. 串行通信是指利用一条传输线将资料一位位地顺序传送.特点是通信线路简单,利用简单的线缆就可实现通信,降低成本,适用于远距离通信,但传输速度慢的应用场 ...
- par函数的las参数-控制x轴和y轴标签的方向
las 参数控制x轴和y轴的刻度线上的标签与两条轴的防线,可选值为0,1,2,3 0为默认值,代表始终与刻度线平行,代码示例: par(las = 0) plot(1:5, 1:5, main = & ...