【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 ...
随机推荐
- mfc小工具开发之定时闹钟之---时间获取和音频播放
1.这里的音频文件是指*.wav格式的音频,参考了飞雪的音频文件,版本号: 飞雪桌面日历 v2.84.1025 绿色版 http://www.piaodown.com/down/soft/18512. ...
- ORACLE 10g RAC [ CSSCLNT][1]clsssInitNative: connect failed, rc 9
"[ CSSCLNT][1]clsssInitNative: connect failed, rc 9“是Oracle RAC中经常遇到的问题,主要出现在安装过程和启动过程中.经常在$ORA ...
- [Hadoop]安装
1 从官网下载hadoop稳定版 http://www.apache.org/dyn/closer.cgi/hadoop/common/ 2 安装JAVA 参考如下blog http://www.cn ...
- iOS -转载-开发之个人开发者账号转公司开发者账号
ps : 个人开发者账号升级公司开发者账号的话需要账号开启双重认证,没有开启的话需要开启(不然走到可以升级的那步的话,点击update升级会提示为了安全起见需要账号开启双双重认证,反正我走到upd ...
- 我如何让echarts实现了分组(原创插件echarts.group代码分享)
前言 echarts是百度出品的一款很棒的前端图表控件,被评为“百度少有的良心产品”.可以实现散点图.折线图.柱状图.地图.饼图.雷达图.K线图等等几十种常用.不常用的图表,效果酷炫. 示例地址:ht ...
- Spring框架中的AOP技术----配置文件方式
1.AOP概述 AOP技术即Aspect Oriented Programming的缩写,译为面向切面编程.AOP是OOP的一种延续,利用AOP技术可以对业务逻辑的各个部分进行隔离,从使得业务逻辑各部 ...
- @Override must override a superclass method 有关问题解决
1.Java开发环境时 如果在使用Eclipse开发Java项目时,在使用 @Override 出现以下错误: The method *** of type *** must override a s ...
- 拨打电话<a href="tel:">跳转到邮件<a href="mailto:">
拨打电话 <a href="tel:0571866000">0571-866000</a> 跳转到邮件 <a href="mailto:jo ...
- Windows(7)上那些好用的软件及优化技巧(原创)
*本文为原创内容,转载请注明作者和出处:www.cnblogs.com/wang1024 软件篇 注:以下软件在百度直接搜索软件名均可找到官网,直接官网下载即可 大众的软件哪个好: 杀毒软件专题 基于 ...
- Fiddler 抓包工具使用详解
博客园 首页 新随笔 联系 订阅 管理 随笔 - 20 文章 - 0 评论 - 64 Fiddler 抓包工具总结 阅读目录 1. Fiddler 抓包简介 1). 字段说明 2). Statis ...