【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 ...
随机推荐
- 【问题】SUSE已经安装了libsodium,安装zeromq时出现下面的错误?
1.[问题]SUSE已经安装了libsodium,安装zeromq时出现下面的错误? checking for libsodium... no configure: error: Package re ...
- java网络编程5-SSL
服务器端 System.out.println("等待客户端连接..."); File keyFile=new File("C:/Users/mu/Desktop/mu. ...
- "_dns_free_resource_record", referenced from:问题
本文转载至 http://blog.csdn.net/woaifen3344/article/details/41309471 _dns_free_resource_r_dns_free环信SDK集成 ...
- sizeWithFont:方法使用明细
个人总结: Computing Metrics for a Single Line of Text– sizeWithFont: 同下面,换行方式默认取NSLineBreakByWordWrappin ...
- jeesite学习笔记(一) 项目框架
JeeSite是基于多个优秀的开源项目,高度整合封装而成的高效,高性能,强安全性的开源Java EE快速开发平台. 在github上,对jeesite平台有详细的介绍,这里稍作整理,给出项目的内置功能 ...
- 俄罗斯方块——shell
#!/bin/bash # Tetris Game # xhchen<[email]xhchen@winbond.com.tw[/email]> #APP declaration APP_ ...
- 【BZOJ4896】[Thu Summer Camp2016]补退选 Trie树
[BZOJ4896][Thu Summer Camp2016]补退选 Description X是T大的一名老师,每年他都要教授许多学生基础的C++知识.在T大,每个学生在每学期的开学前都需要选课,每 ...
- ubuntu 下 Nginx相关设置
ubuntu安装Nginx之后的文件结构大致为: 所有的配置文件都在/etc/nginx下,并且每个虚拟主机已经安排在了/etc/nginx/sites-available下 启动程序文件在/usr/ ...
- 『浅入深出』MySQL 中事务的实现
在关系型数据库中,事务的重要性不言而喻,只要对数据库稍有了解的人都知道事务具有 ACID 四个基本属性,而我们不知道的可能就是数据库是如何实现这四个属性的:在这篇文章中,我们将对事务的实现进行分析,尝 ...
- window下使用mysql,报未定义标识符"SOCKET"
解决方法一: 这个错误是在VC中使用MySQL数据库时出现在mysql_com.h文件中的 my_socket fd; 说明未my_socket未定义,这时只需要在引用mysql.h头文件之前引用# ...