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 ...
随机推荐
- windows和Linux查看端口占用进程并关闭
Windows: 以端口8080为例: 1.查找对应的端口占用的进程:netstat -aon|findstr "8080" ,找到占用8080端口对应的程序的PID号: ...
- if no 和 if not
x = None if x is not None: print("kong") if not x is not None: print("kong") # i ...
- kill -HUP pid 更改配置后不重新启动服务,动态更新配置文件
kill -HUP pid kill -HUP pid pid 是进程标识.如果想要更改配置而不需停止并重新启动服务,请使用该命令.在对配置文件作必要的更改后,发出该命令以动态更新服务配置. 根据约 ...
- 开源直播OBS ( Open Broadcaster Software ) 多机位,多场景切换
项目主页:https://obsproject.com/download 软件是基于MFC的,下载源码后一次性就可以编译通过,使用很简单. 使用了以下开源库: x264 ...
- sparkr——报错
> sc <- sparkR.init() Re-using existing Spark Context. Please stop SparkR with sparkR.stop() o ...
- CSS控制显示图片的一部分
使用情形:防止反复请求图片资源,我们经常采用一张图片多种效果或内容显示. 假设我有纸张竖直方向的一张图片,竖直y轴方向分别是字母:A,B,C.... 现在分别要显示A.B.C 等字母,我们的CSS可以 ...
- [Cxf] spring-cxf 配置
1.在web.xml中配置servlet <!-- spring监听的配置 --> <listener> <listener-class>org.springfra ...
- 自然语言交流系统 phxnet团队 创新实训 项目博客 (一)
2D文字聊天界面大致预期实现文字输入.发送消息.接收消息.你可以通过点击按钮让机器人开启聊天模式或者学习模式.又或是进入3D语音聊天界面或者退出. 目背景 (1) 开发动机的形态 随着科技的进步与生活 ...
- SEO之基于thinkphp的URL伪静态
最近基于thinkphp开发了个导购网站,现在有时间,将遇到的伪静态问题整理下,与大家分享.1.设置URL伪静态在config.ini.php中设置,如果只想前台URL伪静态,那么只在前台的confi ...
- JAVA中String字符串比较equals()和equalsIgnoreCase()的区别
标签: equals和equalsIgnoreC 2012-11-11 16:03 65644人阅读 评论(0) 收藏 举报 分类: JAVA(3) 1.使用equals( )方法比较两个字符串是 ...