159. Find Minimum in Rotated Sorted Array 【medium】
159. Find Minimum in Rotated Sorted Array 【medium】
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.
Notice
You may assume no duplicate exists in the array.
Given [4, 5, 6, 7, 0, 1, 2] return 0
解法一:
class Solution {
public:
/*
* @param nums: a rotated sorted array
* @return: the minimum number in the array
*/
int findMin(vector<int> nums) {
int start = ;
int end = nums.size() - ;
while (start + < end) {
int mid = start + (end - start) / ;
//1 2 3 4 5 6
if (nums[mid] > nums[start] && nums[mid] < nums[end]) {
end = mid;
}
// 3 4 5 6 1 2
else if (nums[mid] > nums[start] && nums[mid] > nums[end]) {
start = mid;
}
// 5 6 1 2 3 4
else if (nums[mid] < nums[start] && nums[mid] < nums[end]) {
//start = mid;
end = mid;
}
}
return nums[start] > nums[end] ? nums[end] : nums[start];
}
};
注意分数组的奇偶去考查。比如[1, 2, 3, 4, 5]和[1, 2, 3, 4, 5, 6]是不同的。
解法二:
public class Solution {
/**
* @param nums: a rotated sorted array
* @return: the minimum number in the array
*/
public int findMin(int[] nums) {
if (nums == null || nums.length == ) {
return -;
}
int start = , end = nums.length - ;
int target = nums[nums.length - ];
// find the first element <= target
while (start + < end) {
int mid = start + (end - start) / ;
if (nums[mid] <= target) {
end = mid;
} else {
start = mid;
}
}
if (nums[start] <= target) {
return nums[start];
} else {
return nums[end];
}
}
}
大神解法就是给力!
159. Find Minimum in Rotated Sorted Array 【medium】的更多相关文章
- 62. Search in Rotated Sorted Array【medium】
62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...
- 【Lintcode】159.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 ...
- [OJ] Find Minimum in Rotated Sorted Array
LintCode 159. Find Minimum in Rotated Sorted Array (Medium) LeetCode 153. Find Minimum in Rotated So ...
- [OJ] Find Minimum in Rotated Sorted Array II
LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...
- 【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】153. Find Minimum in Rotated Sorted Array 解题报告(Python)
[LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现
一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II (3 solutions)
Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...
随机推荐
- Ze_Min Tree 主席树
前言 主席树,也叫可持久化线段树,所以他的本质是颗线段树,而可持久化指的是这颗线段树可以访问过去某个时刻线段树上的信息. 应用 应用的比较多的是查询区间的第k大值(因为其他的数据结构不好做). 实现 ...
- [BZOJ1513]Tet-Tetris 3D
get了新的标记永久化技能- 这题要求询问max和覆盖,因为是线段树套线段树,所以内外都不可以标记下传 这种标记永久化的套路是维护两个标记:$mx,all$,$mx$表示这个子树内的真最大值,$all ...
- 【分块】bzoj2957 楼房重建
http://www.cnblogs.com/wmrv587/p/3843681.html ORZ 分块大爷.思路很神奇也很清晰. 把 块内最值 和 块内有序 两种良好的性质结合起来,非常棒地解决了这 ...
- 【可持久化Trie】模板
总算找到个能看懂的了,orz Lavender. #define INF 2147483647 #define N 100001 #define MAXBIT 31 int root[N],ch[N* ...
- 5.5(java学习笔记)TreeSet和TreeMap
1.TreeMap TreeMap是可排序的Map类,使用这个类时,TreeMap会对存放的数据进行排序. 排序是根据key来排序的,排序规则是key实现comparable接口中的compareTo ...
- iOS开发——检测屏幕旋转
步骤一.注册通知 1: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrien ...
- Shader and Program编程基本概念 - 转
原地址:http://blog.csdn.net/myarrow/article/details/7737313 一.本文关注的问题: • Shader and program 对象介绍• 创建并编译 ...
- Android Studio Emulator 提示 “/dev/kvm is not found” 解决办法
重新安装HAXM即可解决 1.确定已经安装HAXM SDK Manager -> Extras -> Intel x86 Emulator Accelerator (HAXM instal ...
- Jenkins任务优先分配到原来的执行节点上
Jenkins一般部署采用master-salve架构,这样可以同时构建多个任务. jenkins任务构建时默认使用第一次随机分配的机器,这样可以增量拉取代码,减少gitlab或者github的负担. ...
- 利用“进程注入”实现无文件复活 WebShell
引子 上周末,一个好兄弟找我说一个很重要的目标shell丢了,这个shell之前是通过一个S2代码执行的漏洞拿到的,现在漏洞还在,不过web目录全部不可写,问我有没有办法搞个webshell继续做内网 ...