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 ...
随机推荐
- 百度echarts地图扩展动态加载geoCoord
var data={}; for(var i=0;i<result.length;i++){ data[(""+result[i].name+"")]=e ...
- Calculator(1.5)
Calculator(1.5) Github链接 ps.负数的处理未完成 解题过程中遇到的困难和解决 <stack>的使用: 认真研究了栈,基本上掌握了用法,与<queue>的 ...
- equals和“==”
Integer a = new Integer("3"); Integer b = new Integer(3); System.out.println(a==b); System ...
- RadioButtonList 属性设置
RadioButtonList 属性里有RepeatDirection 设为Horizontal
- android 保存Bitmap 到本地 哦
public String saveImage(Bitmap bmp) { File appDir = new File(Environment.getExternalStorageDirectory ...
- svn co
svn co 的用法经常有两种: 第一种: 直接 svn co http://svnserver/mypro/trunk 此时, 会在你的当前目录 ...
- CSS3 Gradient
CSS3CSS3发布很久了,现在在国外的一些页面上常能看到他的身影,这让我羡慕已久,只可惜在国内为了兼容IE,让这一项技术受到很大的限制,很多Web前端人员都望而止步.虽然如此但还是有很多朋友在钻研C ...
- 安全协议系列(四)----SSL与TLS
当今社会,电子商务大行其道,作为网络安全 infrastructure 之一的 -- SSL/TLS 协议的重要性已不用多说.OpenSSL 则是基于该协议的目前应用最广泛的开源实现,其影响之大,以至 ...
- vs2013源码编译zlib 1.2.8
1.从 zlib 官网上下载 zlib最新版 1.28 的源码,解压到 zlib-1.2.8 2.使用vs2013打开vc11目录下的sln工程文件(进行单向升级) 3.修改zlibvc工程属性--& ...
- Android深度探索--HAL与驱动开发----第十章读书笔记
printk函数的用法于-printf 函数类似,只不过printk函数运行在内核空间, printf函数运行在用户空间.也就是说,像Linux 驱动这样的Linux内核程序只能使用printk 函数 ...