【Leetcode】【Medium】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.
解题思路:
一眼就看出o(n)的解法,所以题目是为了考察用二分搜索实现o(logn)的解法;
当num[start] < num[end]时,说明原数列没有旋转,或者整旋转了一圈回到了原点,所以num[start]是最小值;
同理,只要是首比尾小,都表示有序,那么最小值一定在乱序里,这样就可以用mid去中间值,做判断,每次缩小一半范围;
注意:
不用每次都用递归,使用start和end表示操作的下标就可以了;
中值mid = (start+end)/2,mid的值可能等于start,但是不会等于end,所以每次start=mid+1 或者 end = mid,就一定能保证每次更新下标值,不会陷入死循环;
代码:
class Solution {
public:
int findMin(vector<int> &num) {
int start = ;
int end = num.size() - ; while (start < end) {
if (num[start] < num[end])
return num[start];
int mid = (start + end) / ;
if (num[start] <= num[mid])
start = mid + ;
else
end = mid;
} return num[start];
}
};
【Leetcode】【Medium】Find Minimum in Rotated Sorted Array的更多相关文章
- 【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 ...
- 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 u ...
- 【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 ...
- 【LeetCode】153. Find Minimum in Rotated Sorted Array (3 solutions)
Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you ...
- leetcode 【 Find Minimum in Rotated Sorted Array II 】python 实现
题目: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? W ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 在旋转数组中找最小数
Add Date 2014-10-15 Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some p ...
- 【刷题-LeetCode】154 Find Minimum in Rotated Sorted Array II
Find Minimum in Rotated Sorted Array II Suppose an array sorted in ascending order is rotated at som ...
随机推荐
- Ubuntu 16.04 compare 软件安装
1软件官网 http://www.scootersoftware.com/download.php?zz=kb_linux_install 2.打开上述网址可看到安装信息 终端安装方式: wget h ...
- AngularJs学习笔记--Injecting Services Into Controllers
原版地址:http://docs.angularjs.org/guide/dev_guide.services.injecting_controllers 把service当作被依赖的资源加载到con ...
- Junit处理异常
当一个被测类中有异常时,如何处理? 如:一个原始的被测类; public class UserExceptionDemo { public int age; public String name; p ...
- Android学习系列--App列表之拖拽ListView(下)
接着上篇Android学习系列(10)--App列表之拖拽ListView(上)我们继续实现ListView的拖拽效果. 7.重写onTouchEvent()方法. 在这个方法中我们主要是处理 ...
- 使用ant宏定义任务
基础basic.xml文件 <?xml version="1.0" encoding="UTF-8"?><project> < ...
- Bootstrap使用模态框modal实现表单提交弹出框
Bootstrap 模态框(Modal)插件 模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等.如果 ...
- IE10 CSS Hack(顺便聊聊IE11的CSS Hack)
一.特性检测:@cc_on 我们可以用IE私有的条件编译(conditional compilation)结合条件注释来提供针对ie10的Hack:该脚本里面的IE排除条件注释,以确保IE6-9不承认 ...
- DIY了一下自己blog的UI
当年才学前端时就想改自己blog的UI,然鹅当时没看见那个“申请JS权限”,一直以为blog不能随意DIY样式,只改了少许CSS.现在重新看看设置管理选项,简单修改了一下样式(注意:修改样式之前发邮件 ...
- Node.js学习笔记(三) --- package.json 及cnpm
一.包 Nodejs 中除了它自己提供的核心模块外,我们可以自定义模块,也可以使用第三方的模块.Nodejs 中第三方模块由包组成,可以通过包来对一组具有相互依赖关系的模块进行统一管理. 完全符合 ...
- HTML DOM status 属性
<!DOCTYPE html><html> <head>HTML DOM status 属性</head><body><script ...