LintCode 159. Find Minimum in Rotated Sorted Array (Medium)

LeetCode 153. Find Minimum in Rotated Sorted Array (Medium)

这题看着简单, 但是条件总容易搞错. @_@...

思路是在当前区间有序的时候立即停止, 然后某个点(详见代码)就是答案.

我没有做nums.size() == 0的判断, 因为这种情况应该抛个异常什么的, 给什么int值都可能是有效值(如果nums中的数没有指定范围).

解法1

这个解法要注意一种情况, 就是如果进行R = M - 1之后数组有序了, 最小点应该在R + 1处.

class Solution {
public:
int findMin(vector<int> &num) {
int n = num.size();
int L = 0, R = n - 1;
while (L <= R) {
int M = (R - L) / 2 + L;
if (num[M] > num[R]) {
L = M + 1;
} else if (num[M] < num[L]) {
R = M - 1;
} else break;
}
return R + 1 < n && num[R + 1] < num[L] ? num[R + 1] : num[L];
}
};

解法1.1

根据上面的分析, 发现那么只进行R = M就可以避免上面的情况了, 于是又写了个更精简的版本.

九章的解法中, target可以换做num[R], 循环条件换成while(L < R), 返回值直接写成num[L], 其实就是这个解法的无break版本, 循环次数会比这个解法多几次, 因为它要一直找到L == R才会结束.

class Solution {
public:
int findMin(vector<int> &num) {
int n = num.size();
int L = 0, R = n - 1;
while (L < R) {
int M = (R - L) / 2 + L;
if (num[M] > num[R]) {
L = M + 1;
} else if (num[M] < num[L]) {
R = M;
} else break;
}
return num[L];
}
};

解法1.2

这篇博文启发, 判断"是否有序"可以放到while的判断条件中.

class Solution {
public:
int findMin(vector<int> &num) {
int n = num.size();
int L = 0, R = n - 1;
while (L < R && num[L] > num[R]) {
int M = (R - L) / 2 + L;
if (num[M] > num[R]) {
L = M + 1;
} else {
R = M;
}
}
return num[L];
}
};

时间复杂度: O(logn)

空间复杂度: O(1)

[OJ] Find Minimum in Rotated Sorted Array的更多相关文章

  1. [OJ] Find Minimum in Rotated Sorted Array II

    LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...

  2. LeetCode OJ 154. Find Minimum in Rotated Sorted Array II

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  3. leetcode 【 Find Minimum in Rotated Sorted Array II 】python 实现

    题目: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? W ...

  4. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  5. [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 ...

  6. 【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 ...

  7. 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 ...

  8. LeetCode Find Minimum in Rotated Sorted Array II

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...

  9. LeetCode Find Minimum in Rotated Sorted Array

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...

随机推荐

  1. SWFObject2.0

    PROBLEM: using % in the height and width(e.g. 100%) with the SWFObject 2.0 (or 2.1) with dynamic pub ...

  2. Webstorm 不识别es6 import React from ‘react’——webstorm不支持jsx语法怎么办

    2016-10-31更新 webstorm不支持es6语法怎么办? webstorm不支持jsx语法怎么办? 参考:webstorm不支持jsx语法怎么办 I spent ages trying to ...

  3. MVC小系列(二十一)【带扩展名的路由可能失效】

    mvc3之后:如果路由带上扩展名,比如这样: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRout ...

  4. MongoDB中的分组

    一.MongoDB中的Count函数.Distinct函数以及分组 准备工作,插入一个班级的文档 > for(var i=0;i<10;i++){ ... db.Classes.inser ...

  5. C#学习笔记(3)

    先理解一下方法重写和方法重载这2个概念: 1.方法重写(override):发生在父子类之间,子类重写父类中的方法,关键字是override. 2.方法重载(overload):一个类中有多个重名的方 ...

  6. Object-C属性(Properties)

    前面我们写了caption和photographer的访问方法,你可能也注意到了,那些代码很简单,应该可以写成具有更普遍意义的形式. 属性是Object-C的一个特性,它允许我们自动生成访问器,同时还 ...

  7. IOS 本地通知

    操作流程 1.接收通知 2.注册发送通知 用途:提示时间,闹钟 //接收本地通知(在Appdelegate里面实现) - (void)application:(UIApplication *)appl ...

  8. 2016.08.07计算几何总结测试day2

    T1 bzoj: [Usaco2010 OPen]Triangle Counting 数三角形 看到这个题n那么大, 于是想到极角排序搞一搞,然而排完序后立马懵逼,完全不知道接下来应该怎么写.... ...

  9. ZeroMemory和memset的区别

    摘自百度百科,保存为学习使用 ZeroMemory,是美国微软公司的软件开发包SDK中的一个宏. 其作用是用0来填充一块内存区域. 声明 void ZeroMemory( PVOID Destinat ...

  10. Docker命令使用详解

    其中<>括起来的参数为必选, []括起来为可选 docker -exec -i -t 3f407013d8c0 /bin/bash    进入容器 docker version查看dock ...