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 ...
随机推荐
- 【 2013 Multi-University Training Contest 4 】
HDU 4632 Palindrome subsequence dp[x][y]表示区间[x,y]构成回文串的方案数. 若str[x]==str[y],dp[x][y]=dp[x+1][y]+dp[x ...
- 关于StatusStrip控件和StatusBar控件的小试
今天,在网上查找资料,突然看到一个例子,但例子中提及的StatusBar控件,我发现在vs控件压根不存在,我就郁闷了,于是上网查找才知道,现在这个控件已经被StatusStrip控件给吞了,Statu ...
- JS中函数的调用和this的值
调用每一个函数会暂停当前函数的执行,传递控制权和参数给新函数.除了声明时定义的形式参数,每个函数还接收两个附加的参数:this 和 arguments. 参数this在面向对象编程中非常重要,他的值取 ...
- idea 中利用maven创建java web 项目
转自:http://www.linuxidc.com/Linux/2014-04/99687.htm 本文主要使用图解介绍了使用IntelliJ IDEA 12创建Maven管理的Java Web项目 ...
- sql 2008 修改链接服务器 Rpc &Rpc Out
From: http://blog.csdn.net/gnolhh168/article/details/41725873 USE [master] GO EXEC master.dbo.sp_ser ...
- NodeJs使用asyncAwait两法
async/await使用同步的方式来书写异步代码,将异步调用的难度降低到接近于0,未来必将大放异彩.然而在当下,由于标准化的缓存步伐,async/await尚在ES7的草案中.为了尝先,特试用了下面 ...
- shell 问题解决
如果想找常用指令,请点击图片 shell 脚本中的指令,在不确定情况下,不要随意使用nohup,否则也许会造成灾难性后果,比如--内存爆掉 shell 随机函数生成 function random() ...
- 前端开发week3
开发工具学习ing... lesscss 框架 lesscss是一种动态样式语言,属于css预处理语言的一种,它使用类似css的语法,为css的赋予了动态语言的特性,如变量.继承.运算.函数等,更方便 ...
- unity代码加密for Android,mono编译
uinty3d加密推荐几篇比较好的博客链接: http://www.cppcourse.com/u3d-encryption.html http://www.xuanyusong.com/archiv ...
- JDBC driver connection string大全
Database / data source URL format / driver name Value Default port MySQL URL format: jdbc:mysql: ...