【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 might become 4 5 6 7 0 1 2).
Find the minimum element.
You may assume no duplicate exists in the array.
题解:就是找数组中第一个“下凹”的地方,可以遍历,不过也可以用二分法,速度更快。
特别注意边界的处理:如果当前的中间值mid在0这个位置,只要考虑它后面的元素是不是小于它;如果在数组最后的位置上,要考虑它前面的元素是不是大于它。最后当发现当前mid所指的值不是所求的“下凹点”的时候,要把它和最后数组的最后一位比较,如果它比数组的最后一位大,说明要找的“下凹点”在mid所指位置的后面,否则在mid所指位置的前面。
JAVA版本代码如下:
import java.awt.datatransfer.StringSelection;
public class Solution {
public static void main(String[] args){
int num[] = {3,4,5,1,2};
Solution s = new Solution();
System.out.println(s.findMin(num));
}
public int findMin(int[] num) {
int begin = 0;
int end = num.length-1;
while(begin <= end){
int mid = (begin + end)/2;
if(mid == 0){
if(mid+1 < num.length && num[mid+1] < num[mid])
return num[mid+1];
else {
return num[0];
}
}
if(mid == num.length-1){
if(mid-1 >=0 && num[mid-1] > num[mid])
return num[mid];
else {
return num[0];
}
}
if(num[mid-1] > num[mid] && num[mid+1] > num[mid])
return num[mid];
if(num[num.length-1] < num[mid])
begin = mid+1;
else {
end = mid-1;
}
}
return num[0];
}
}
【leetcode刷题笔记】Find Minimum in Rotated Sorted Array的更多相关文章
- 【leetcode刷题笔记】Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【leetcode刷题笔记】Search 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刷题笔记】Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现。
简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现. 题目: Suppose a sorted array is rotated at som ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(153) Find Minimum in Rotated Sorted Array
题目 Total Accepted: 65121 Total Submissions: 190974 Difficulty: Medium Suppose a sorted array is rota ...
- 【leetcode刷题笔记】Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 【leetcode刷题笔记】Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- (python)leetcode刷题笔记04 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...
随机推荐
- 数据挖据之GeoHash核心原理解析
引子 机机是个好动又好学的孩子,平日里就喜欢拿着手机地图点点按按来查询一些好玩的东西.某一天机机到北海公园游玩,肚肚饿了,于是乎打开手机地图,搜索北海公园附近的餐馆,并选了其中一家用餐. 饭饱之后机机 ...
- java ssh介绍(1)
今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以现在公司很多web项目的控制层的技术框架由struts2迁移到springMVC,我突然有了一个新的疑 ...
- Hadoop2的HA安装(high availability):JournalNode+ zookeeper
前面介绍过使用NFS+zookeeper来解决namenode单点失败问题,因为NFS可能也会存在单点问题,所以hadoop提供了一种叫做JournalNode技术,这项技术可以在JournalNod ...
- Chem 3D软件可以改变背景吗
化学绘图过程中常常需要绘制三维结构的图形,Chem 3D软件是ChemOffice套件中专门用于绘制三维结构的组件.用过它的用户会发现,其背景颜色通常都默认为深蓝色,但是不是每个场景都适合用深蓝色的背 ...
- cout显示Mat类对象报错Access Violation
AV(Access Violation)错误:非法访问. image_match.exe 中的 0x0000002a 处有未经处理的异常: 0xC0000005: Access violation 程 ...
- Microsoft Visual C++ 2005 Redistributable---win下安装软件“嘭”的一声报错!
今天下了个MindManager,正准备安装结果出现了如题的错误提示!!! 于是百度/google一下,在权威的微软官网下找到了答案,他妈的,看了之后表示很无奈 If the non unicode ...
- vmware 虚拟机下 ubuntu 与主机共享锐捷
一直以来.想要学习 linux ,在 vm 虚拟机下安装了 ubuntu 系统. 可是这个系统并不能上网.原因就是 vm 虚拟机的虚拟网卡会和锐捷冲突.锐捷会检測到多网卡,断开网络,所以不得不禁用 v ...
- std::deque
deque容器为一个给定类型的元素进行线性处理,像向量一样,它能够快速地随机访问任一个元素,并且能够高效地插入和删除容器的尾部元素.但它又与vector不同,deque支持高效插入和删除容器的头部元素 ...
- Android实例-退出程序
Android实例-退出程序 http://www.cnblogs.com/FKdelphi unit Unit1; interface uses System.SysUtils, System.Ty ...
- db2 相关命令
db2ilist ---列出主机所有实例 db2icrt instanceName ---创建实例名为instanceName的实例 db2idrop instanceName ---删 ...