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

    public List groupBy(List list,String flag,String... sortName) throws Exception{ Map<String,List&l ...

  2. js获得文件根目录

    function getRootPath(){ //获取当前网址,如: http://localhost:8083/proj/meun.jsp var curWwwPath = window.docu ...

  3. Using Run-Time Dynamic Linking(使用运行时动态链接库)

    // A simple program that uses LoadLibrary and // GetProcAddress to access myPuts from Myputs.dll. #i ...

  4. 暑假集训(1)第四弹 -----Find a way(Hdu2612)

    Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Nin ...

  5. Constants in C++

    The first motivation for const seems to have been to eliminate the use of preprocessor #define for v ...

  6. IOS 学习笔记 2015-03-24 OC-API-网络访问-案例一

    // // WPSuggest.h // OC-API-网络访问 // // Created by wangtouwang on 15/3/24. // Copyright (c) 2015年 wan ...

  7. 【实习记】2014-09-26恢复linux下误删的ntfs盘中的文件

        情景,ubuntu下把NTFS格式的盘中的“实习记”文件夹彻底删除了,追毁莫及,粗心觉不是一件好的事情. linux下回复ntfs盘下的文件不能用ext3grep,而使用debugfs命令实在 ...

  8. wamp集成环境php多版本搭建(php5.5,php5.6,php7.0.6)

        首先需要搭建的版本可以在php官方(http://windows.php.net/download)下载对应的版本,X86对应的是32位操作系统,X64对应的是64位操作系统.    1:下载 ...

  9. 网站开发常用jQuery插件总结(五)滚动条插件nanoscroller

    网站在展示信息时,如果信息量过大,解决方法主要有三种.1.分页,将信息分页显示.2.整页显示,但是页面过长,影响浏览体验.3.使用滚动条,而默认滚动条样式太单一,用户体验不友好.所以我们需要美化滚动条 ...

  10. BOM 子对象,history,location,screen

    history:包括浏览器访问过的 url 属性:返回浏览器访问过的历史记录数 方法:back(); forward(); go(number) location:包含当前 url 的相关信息 属性: ...