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 就是找到第一个违反升序的值,就 ...
随机推荐
- 12. Linux在线升级yum
软件包仓库源 将yum源放在/etc/yum.repo.d root@cfm880 Packages]# cd /etc/yum.repos.d/[root@cfm880 yum.repos.d]# ...
- Lighttpd虚拟主机和多域名的配置
lighttpd是个轻巧的web服务器组件,在linux系统上较为流行,功能能满足一般网站的需求(虚拟主机,伪静态,防盗链等),而跑起来负载却低于apache. 一般网站地址的形式为www.xxx.c ...
- go特性学习
而后我写了一些测试代码: dept1 := Dept{ name: "MySohu", building: "Intern ...
- 点击弹出固定大小的新窗口(js实现)
<SCRIPT LANGUAGE="javascript"> <!-- window.open ('page.html') --> </SCRIPT& ...
- uniDBGrid导入数据库(转红鱼儿)
有朋友问如何将excel导入数据库,这是我做的uniGUI项目中代码,实现uniDBGrid导入数据库的函数,因为用了kbmMW,所以你看到是将uniDBGrid导入kbmMWClientQuery, ...
- QtCreator动态编译jsoncpp完美支持x86和arm平台
如果是做嵌入式开发. 在Qt下支持JSon最好的办法,可能不是采用qjson这个库.QJson这个库的实例只提供了x86环境下的编译方法. Installing QJson-------------- ...
- c 数据拼接
char buf1[] = {0x31,0x32,0x33,0x00,0x51,0x52,0x53,0xaa,0xbb,0xcc,0x00}; int a=0xabcd6799; int b=0x88 ...
- 这是一个比较清晰的SSH框架结构,有兴趣的博友可以看一下
http://www.cnblogs.com/zhaowancheng/p/5841591.html
- js中的引用类型-object
- udp通信的原理---makefile文件
由于UDP通信不需要事先建立连接,因此不需要TCP中的connect函数. 服务器端的步骤如下: 1. socket: 建立一个socket 2. bind: 将这个soc ...