今天看到LeetCode OJ题目下方多了“Show Tags”功能。我觉着挺好,方便刚開始学习的人分类练习。同一时候也是解题时的思路提示。

【题目】

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4
5 6 7 0 1 2
).

Find the minimum element.

You may assume no duplicate exists in the array.

【解法】

题目比較简单,直接看代码吧,可是要想把代码写得美丽并不easy啊。

O(n)非常好写,O(lgn)要好好捋捋思路。

public class Solution {
// O(n) simple
public int findMin1(int[] num) {
int len = num.length;
if (len == 1) {
return num[0];
} for (int i = 1; i < len; i++) {
if (num[i] < num[i-1]) {
return num[i];
}
} return num[0]; // 尼玛,看成找中间数了
// if (len % 2 != 0) { //len is odd
// return num[(begin+len/2)%len];
// } else { //len is even
// return (num[(begin+len/2-1)%len] + num[(begin+len/2)%len]) / 2;
// }
} // O(lgn) not that good
public int findMin2(int[] num) {
int len = num.length;
if (len == 1) return num[0]; int left = 0, right = len-1;
while (left < right) {
if ((right-left) == 1) return Math.min(num[left], num[right]); if (num[left] <= num[right]) return num[left]; int mid = (left + right) / 2;
if (num[mid] < num[right]) {
right = mid;
} else if (num[left] < num[mid]) {
left = mid;
}
} return num[left];
} // O(lgn) optimized iteratively
public int findMin3(int[] num) {
int len = num.length;
if (len == 1) return num[0];
int left = 0, right = len-1;
while (num[left] > num[right]) { // good idea
int mid = (left + right) / 2;
if (num[mid] > num[right]) {
left = mid + 1;
} else {
right = mid; // be careful, not mid-1, as num[mid] maybe the minimum
}
}
return num[left];
} // O(lgn) optimized recursively
public int findMin(int[] num) {
return find(num, 0, num.length-1);
} public int find(int[] num, int left, int right) {
if (num[left] <= num[right]) {
return num[left];
}
int mid = (left + right) / 2;
if (num[mid] > num[right]) {
return find(num, mid+1, right);
}
return find(num, left, mid);
}
}

【LeetCode】Find Minimum in Rotated Sorted Array 解题报告的更多相关文章

  1. 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)

    [LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...

  2. LeetCode 新题: Find Minimum in Rotated Sorted Array 解题报告-二分法模板解法

    Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...

  3. LeetCode: Search in Rotated Sorted Array 解题报告

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  4. Leetcode Find Minimum in Rotated Sorted Array 题解

    Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...

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

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

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

  7. LeetCode Find Minimum in Rotated Sorted Array II

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

  8. LeetCode Find Minimum in Rotated Sorted Array

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

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

随机推荐

  1. (转)Oracle中判断某字段不为空及为空的SQL语句

    比如 insert into table a (a1,b1)values("a1",''); 对于这种情况,因为表里存的是'',其实是没有内容的,要查询这个字段,不能直接使用 se ...

  2. Linux添加用户并赋予/取消管理员权限

    Ubuntu sudo adduser username # 添加用户 sudo adduser username sudo # 追加管理员权限 grep -Po '^sudo.+:\K.*$' /e ...

  3. Linux管道符、重定向与环境变量

    ——<Linux就该这么学>笔记 输入输出重定向输入重定向 指把文件导入到命令中输出重定向 指把原本要输出到屏幕的数据信息写入到指定文件中 输出重定向 分为标准输出重定向和错误输出重定向 ...

  4. eclipse断点调试时不能进入断点调试

    页面JavaScript代码有错误!!!F12调试.

  5. php7使用curl

    /** * @param string $url * @return mixed */ public function doGet($url) { //初始化 $ch = curl_init(); c ...

  6. 实战WCF中net.tcp和net.msmq绑定协议

    平时很少写博文的,以前都是转载其他园友的文章,这几天有时间就自己尝试写一些wcf相关的文章,希望能给有需要的人带来一点帮助吧,水平有限再加上初次动手,写得不好还请多多包含!废话不多说了直接进入正题. ...

  7. (五)agentd端cpu的触发器配置

    配置===>模板===>选择对应的模板===> 这里我验证触发器是否有效,定义的触发器的值超过0.01就出发报警,这里我做的是最新的T值超过0.01就触发触发器 验证,说明触发器触发 ...

  8. Mathematica作图

    第2讲 在Mathematica中作图    一个较强的符号计算系统均有很好的绘图功能,Mathematica也不例外,Mathematica 拥有非常强大的绘图功能.并且提供了一大批基本数学函数的图 ...

  9. Python练习——同时安装python2 与 python 3如何选择不同解释器运行脚本

    如果同时安装了python 2 和python 3 那么我们需要在运行时指定解释器 如下: 其中py -2 ex1.py指定了解释器的版本,以及打开的文件 如果使用 py-3 ex1.py则使用了py ...

  10. hdu6231

    hdu6231 题意 给出一些数字,对于任意长度不小于 \(k\) 的区间,把第 \(k\) 大数加入到一个新的数组 \(B\) 中,求 \(B\) 数组第 \(m\) 大数. 分析 二分答案 \(x ...