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 就是找到第一个违反升序的值,就 ...
随机推荐
- oracle 常见恢复
author by :shawnloong 环境:windows 2008 r2 sp1 db:oracle 11g r2 做之前记得做个完整备份 ONFIGURE RETENTION POLICY ...
- sublime ctags
安装 ctags: 下载ctags,复制exe到系统目录或者sublime text的目录中 sublime: 安装ctags插件 使用: 快捷键 Command Key Binding Alt Bi ...
- java中常用数据类型转换器
/** * 把String转换成long * * @param src 要转换的String * @param def 转换失败时返回此值 * @return 转换好的long */ public s ...
- SQL Server添加MDW性能监控报表(转载)
10.2 Data Collector与MDW Data Collection功能是SQL SERVER 2005版本提供的数据库监控报表的功能,通过定时地对数据库的语句运行情况,服务器各种资源的监控 ...
- {CSDN}{英雄会}{砍树、石子游戏}
砍树 思路: 可以将题目意图转化为:给定一棵树,求其中最接近总权值一半的子树. DFS求每个节点的所有子节点的权值和,遍历每个节点,最接近总权值一半的即为答案.复杂度O(N). 石子游戏: 思路: 一 ...
- .net正则表达式
1. "^-?[1-9]\\d*$",//整数 2. "^[1-9]\\d*$", //正整数 3. intege2:"^-[1-9]\\d*$&qu ...
- Android_ADB 常用 shell命令 和 sqlite3 简单增删改查
今天学习了一个ADB的常用命令.接下来简单使用几个常用ADB shell 命令. 首先我们得明白什么是adb.exe ADB -Android Debug Bridge, 是 Android sdk ...
- HTML基础第二天知识点整合
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- easyUI删除行的操作
columns: [[ { field: 'KeyContent', title: '关键词' }, { field: 'ProductType', title: "用品/配件", ...
- jQuery设置和获取以及修改class name值操作
在Web程序开发中.很多时候会用需要修改Html标签的class名称.来达到修改标签样式的效果.那么在代码中一般是怎么操作的呢.本文将为你详细讲解一下class的使用.在jQuery中可以使用attr ...