LintCode Find Minimum In Rotated Sorted Array
1. 画图, 直观。
2. 讨论数组为空或者个数为零。
3. 讨论首尾, 若为翻转过的则进行查找直到最后两个数进行比较, 取小者。
public class Solution {
/**
* @param num: a rotated sorted array
* @return: the minimum number in the array
*/
public int findMin(int[] num) {
// write your code here
if(num == null || num.length == 0) return -1;
// int small = num[0];
// for(int i = 1; i < num.length; i++) {
// //small = (small < num[i]) ? small : num[i];
// if(small > num[i]) small = num[i];
// else small = small;
// }
// return small;
int left = 0; int right = num.length - 1;
if(num[left] < num[right]) return num[left];
else{
while(left + 1 < right){
int mid = (left + right) / 2;
if(num[left] < num[mid]){
left = mid;
}
if(num[left] > num[mid]){
right = mid;
}
}
if(num[left] < num[right]) return num[left];
else return num[right];
}
}
}
LintCode Find Minimum In Rotated Sorted Array的更多相关文章
- [OJ] Find Minimum in Rotated Sorted Array II
LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...
- [OJ] Find Minimum in Rotated Sorted Array
LintCode 159. Find Minimum in Rotated Sorted Array (Medium) LeetCode 153. Find Minimum in Rotated So ...
- [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 I&&II
题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...
- 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
原题链接在这里: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 就是找到第一个违反升序的值,就 ...
- Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
随机推荐
- <java基础学习>01环境变量配置
安装完JDK开始配置系统环境变量,在path变量里面添加java的bin目录 方法二: 配置完成后 在命令下输入javac查看是否配置成功 第一个java程序 hello world! class H ...
- python 版 mldivide matlab 反除(左除)《数学建模算法与程序》Python笔记
今天在阅读数学建模的时候看到了差分那章 其中有一个用matlab求线性的代码,这里我贴出来 这里我送上 Python代码 In [39]: import numpy as np ...: from s ...
- React Native 组件样式测试
View组件默认样式(注意默认flexDirection:'column') {flexGrow:0,flexShrink:0,flexBasis:'auto',flexDirection:'colu ...
- pdsh使用
原文地址:http://bbs.chinaunix.net/thread-2002080-1-1.html 当我们管理数以十计或者更多的集群系统时,往往需要在每台机器上执行同样的命令,或者拷贝同样的文 ...
- SQL数据库,使用事务执行增删改操作,给自己一个后悔的机会
内容并不复杂,使用起来也比较简单. 主要使用以下3条SQL语句: 开始事物:BEGIN TRAN(全拼 TRANSACTION 亦可)提交事物:COMMIT TRAN回滚事务:ROLLBACK TRA ...
- 曲线拟合的最小二乘法(基于OpenCV实现)
1.原理 在现实中经常遇到这样的问题,一个函数并不是以某个数学表达式的形式给出,而是以一些自变量与因变量的对应表给出,老师讲课的时候举的个例子是犯罪人的身高和留下的脚印长,可以测出一些人的数据然后得到 ...
- vps推荐之DigitalOcean
作为一个爱折腾的网站”程序猿“,我用过多家vps,由于一般支持paypal 月付, 所以基本上都会用两三个月,不行就换另一家. 1.Yard VPS 台湾人开的,有中文支持,貌似也支持支付宝付款,偶尔 ...
- 开启GZIP(转)
因为在做一个项目,项目里面服务器主要提供数据,但是数据多了文件就大了,比较浪费流量和时间,我们便用Gzip来处理.我在本机上是apache,服务器上是IIS6.0,用的是php,那么我就在这里分享一下 ...
- goldengate studio 12.2.1.2.6发布
主要特性: 1. 支持bigdata & teradata为目标端:
- 第九章 硬件抽象层:HAL
这一章介绍HAL,全称为Hardware Abstract Layer,即硬件抽象层,它是建立在Linux驱动之上的一套程序库,程序库并不属于Linux内核,而是属于Linux内核层之上的应用层.为A ...