Find Minimum in Rotated Sorted Array leetcode
直接贴代码,这道题是
search in rotated sorted array leetcode
的前面部分!
class Solution {
public:
int findMin(vector<int>& nums) {
if (nums.empty())
return -;
int res = find(nums, , nums.size()-);//好神奇,第二个参数无论减或者不减1,都不影响该题的结果
if (res == -)
return -;
return nums[res];
}
int find(vector<int>& nums, int l, int r)
{
if (l>r || l >= nums.size())
return -;
int mid = (l + r) / ;
if (nums[l] <= nums[mid])
{
int pos = find(nums, mid + , r);
if (pos == -)
return l;
return nums[l]<nums[pos] ? l : pos;
}
else
{
int pos = find(nums, l, mid - );
if (pos == -)
return mid;
return nums[mid]<nums[pos] ? mid : pos;
}
}
};
Find Minimum in Rotated Sorted Array leetcode的更多相关文章
- 153. Find Minimum in Rotated Sorted Array - LeetCode
Question 153. Find Minimum in Rotated Sorted Array Solution 题目大意:给一个按增序排列的数组,其中有一段错位了[1,2,3,4,5,6]变成 ...
- Find Minimum in Rotated Sorted Array leetcode java
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- Find Minimum in Rotated Sorted Array——LeetCode
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- 153. Find Minimum in Rotated Sorted Array(leetcode, binary search)
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary ...
- [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 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 就是找到第一个违反升序的值,就 ...
随机推荐
- Chap3: question: 11 - 18
11. double 数值的整数次方 note: 浮点数表示时有误差,判等时必须自己根据精度要求实现. #include <iostream> #include <ctime> ...
- 验证码I
package com.ah.testjava.validatecode; import java.awt.Color; import java.awt.Font; import java.awt.G ...
- Java正则表达式详解
转自http://edu.yesky.com/edupxpt/18/2143018.shtml
- display:inline-block左右元素上下不对齐
今天做了两个inline-block元素,出现左右两个元素顶端出现上下不对齐的情况(下图): 解决办法: 把应用 inline-block的元素加上 vertical-align: top; .CSS ...
- java三大框架之一hibernate使用入门
综述:Hibernate的作用就是让实体类与数据库映射,使数据持久化,用于替代JDBC,使我们不致于写那么多sql语句代码. 1. 首先在官网www.hibernate.org下载hibernate包 ...
- centos环境下使用percona-xtrabackup对mysql5.6数据库innodb和myisam进行快速备份及恢复
centos环境下使用percona-xtrabackup对mysql5.6数据库innodb和myisam进行快速备份及恢复 有时候我们会碰到这样的业务场景: 1.将大的数据库恢复到本地进行业务测试 ...
- 炫酷的时钟--canvas初体验
先啥也不说:来张效果图 我是根据:http://www.imooc.com/learn/133 这里的课程进行学习的.大大的感谢liuyubobobo老师的深入浅出的讲解!! 我在这里仅仅提供我自己的 ...
- Python PEP 492 中文翻译——协程与async/await语法
原文标题:PEP 0492 -- Coroutines with async and await syntax 原文链接:https://www.python.org/dev/peps/pep-049 ...
- asp.net前台绑定时间格式时,定义时间格式
<%#Eval("news_time","{0:yyyy-MM-dd}") %><%#((DateTime)Eval("news_t ...
- php解析json数组
function urltest(){ $url='http://121.43.153.80:8983/solr/collection1/select?q=+searchkey%3a%E4%BC%9A ...