【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
今天看到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 解题报告的更多相关文章
- 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)
[LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...
- LeetCode 新题: Find Minimum in Rotated Sorted Array 解题报告-二分法模板解法
Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...
- LeetCode: Search in Rotated Sorted Array 解题报告
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- Leetcode Find Minimum in Rotated Sorted Array 题解
Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...
- [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 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 就是找到第一个违反升序的值,就 ...
- 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 ...
随机推荐
- 利用git把本地项目传到github+将github中已有项目从本地上传更新
利用git把本地项目传到github中 1.打开git bash命令行,进入到要上传的项目中,比如Spring项目,在此目录下执行git init 的命令,会发下在当前目录中多了一个.git的文件夹( ...
- 原创 gif png bmp jeg 显示方法
/// <summary> /// 注意不要忘记引用那几个图片单元哦,除了bmp格式不需要引用任何单元, /// 其它图片格式都需要引用对应的图片单元 /// png ---> Vc ...
- k8s的chart学习(上)
chart 是 Helm 的应用打包格式.chart 由一系列文件组成,这些文件描述了 Kubernetes 部署应用时所需要的资源,比如 Service.Deployment.PersistentV ...
- PostgreSQL9.6.3的REDIS测试
安装redis_fdwcd /usr/local/srcgit clone https://github.com/pg-redis-fdw/redis_fdw.gitcd redis_fdw/git ...
- (13)python 正则表达式
匹配单个字符 f. o f和o之间是任意字符 例如:fbo123 .. 任意两个字符 \.用来匹配. 边界匹配 the 表示包含the的任何字符串 ^from 表示以from开头的所 ...
- Opencv利用Mat访问像素值
如果是采用Mat形式存储,想要访问灰度图像的灰度值,可以采用如下方法: 如果是彩色图像,采用如下方法: 说明: 其中gray_value中存放灰度值,image是读入的图像,i表示行,j表示列: co ...
- 2017中国大学生程序设计竞赛 - 女生专场C【前后缀GCD】
C HDU - 6025 [题意]:去除数列中的一个数字,使去除后的数列中所有数字的gcd尽可能大. [分析]: 数组prefixgcd[],对于prefixgcd[i]=g,g为a[0]-a[i]的 ...
- HDU1213 How Many Tables (并查集)
题目大意: 有一个人要过生日了,请他的朋友来吃饭,但是他的朋友互相认识的才能坐在一起,朋友的编号从1 ~ n,输入的两个数代表着这两个人互相认识(如果1和2认识,2和3认识,那么1和3也就认识了).问 ...
- 11.5NOIP2018提高组模拟题
书信(letter) Description 有 n 个小朋友, 编号为 1 到 n, 他们每人写了一封信, 放到了一个信箱里, 接下来每个人从中抽取一封书信. 显然, 这样一共有 n!种拿到书信的情 ...
- 数学结论【p1463】[POI2002][HAOI2007]反素数
Description 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例如,整数 ...