Leetcode- Find Minimum in Rotated Sorted Array-ZZ
http://changhaz.wordpress.com/2014/10/15/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 might become 4 5 6 7 0 1 2).
Find the minimum element.
You may assume no duplicate exists in the array.
Solution:
Classic binary search problem. One place worth noticing is the termination conditions. Whenever our window has only one element, its value is the answer. When our window has two elements, and the first element is larger than the second one, the second element is our answer. This case falls into the num[mid]>=num[start] condition in the code below. (given that there is no duplicates in the array)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class Solution {public: int findMin(vector<int> &num) { int start=0,end=num.size()-1; while (start<end) { if (num[start]<num[end]) return num[start]; int mid = (start+end)/2; if (num[mid]>=num[start]) { start = mid+1; } else { end = mid; } } return num[start]; }}; |
Leetcode- Find Minimum in Rotated Sorted Array-ZZ的更多相关文章
- Leetcode Find Minimum in Rotated Sorted Array 题解
Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...
- [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 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 就是找到第一个违反升序的值,就 ...
- 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
Question Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allo ...
- Leetcode Find Minimum in Rotated Sorted Array I and 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
Description: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 ...
- [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 ...
随机推荐
- nginx实现多语言跳转不同的url
nginx实现多语言跳转不同的url server { listen 80; server_name www.text.com; location / { if ($http_accept_langu ...
- 动态时间规整DTW(Dynamic Time Warping )
动态时间规整DTW(Dynamic Time Warping ) 原文:https://blog.csdn.net/raym0ndkwan/article/details/45614813 算法笔记- ...
- C#反射动态调用dll中的方法,并返回结果(转)
反射的作用是动态的加载某个dll(程序集),并执行该程序集中的某个方法,并返回结果:当然也可以给该方法传递参数 namespace assembly_name { public class assem ...
- less变量插值
在使用less的过程中,我在background的中引用图片路径,希望先确定一个baseurl,然后再在url中使用拼接字符串的方式拼接,尝试多次,失败. 实际上less的变量插值是有自己的一套规则的 ...
- Fiddler模拟发送post请求
fiddler在进行接口测试时,会模拟post请求,发送不同的请求参数,返回不同的结果,今天我们就来分享一下,怎么用Fiddler工具模拟post请求: 打开Fiddler工具,在右侧点击“compo ...
- oracle系统包——DBMS_PIPE用法
DBMS_PIPE包用于在同一例程(实例)的不同会话之间进行通信:注意,如果用户要执行包dbms_pipe中的过程和函数,则必须要为用户授权. sql>conn sys/oracle as sy ...
- Codeforces 156B Suspects——————【逻辑判断】
Suspects Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit St ...
- jQuery 自定义方法(扩展方法)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [转]微信小程序开发:http请求
本文转自:http://www.cnblogs.com/dragondean/p/5921079.html 在微信小程序进行网络通信,只能和指定的域名进行通信,微信小程序包括四种类型的网络请求. 普通 ...
- Tool Scripts
1. Function: 16进制转字符串 Create FUNCTION [dbo].[f_hextostr] (@hexstring VARCHAR(max)) RETURNS VARCHAR(m ...