【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 ...
随机推荐
- Selenium2+python自动化76-Chrome配置加载【转载】
转至博客:上海-悠悠 一.加载Chrome配置 chrome加载配置方法,只需改下面一个地方,username改成你电脑的名字(别用中文!!!) '--user-data-dir=C:\Users\u ...
- eps图片中有中文乱码的问题
一般的,如果matlab中的fig图片中有中文,直接saveas为eps,eps再插入latex后会出现乱码. 解决的办法为: (1) *.fig利用‘file--print’保存为*.pdf (2) ...
- MAC使用homeBrew安装Redis
homeBrew的操作命令如下: brew search ** //查找某个软件包 brew list //列出已经安装的软件的包 brew install ** //安装某个软件包,默认安装的是稳定 ...
- C# 对话框使用大全
对话框中我们常用了以下几种:1.文件对话框(FileDialog) 它又常用到两个: 打开文件对话框(OpenFileDialog) 保存文件对话(SaveFileDialog)2.字体对话框(Fon ...
- 【转】Python模块subprocess
subprocess 早期的Python版本中,我们主要是通过os.system().os.popen().read()等函数.commands模块来执行命令行指令的,从Python 2.4开始官方文 ...
- Python timedelta模块 时间增减用法
timedalte 是datetime中的一个对象,该对象表示两个时间的差值 构造函数:datetime.timedelta(days=0, seconds=0, microseconds=0, mi ...
- python3 while循环及for循环
yueer = 18 count = 0 while count < 3: yueerage = int(input('悦儿多大呢:')) if yueerage == yueer: print ...
- 见微知著(三):解析ctf中的pwn--Fastbin和bins的溢出
1月1号写博客,也是不容易呀!大家新年快乐呀! 先从Fastbin看起,是2015年RCTF的一道pwn题,shaxian.先看看代码的大致流程,随便输入一下: 这个题目关键之处在于堆溢出,对于堆种类 ...
- IDEA的Maven项目找不到class
- [CF930E]/[CF944G]Coins Exhibition
[CF930E]/[CF944G]Coins Exhibition 题目地址: CF930E/CF944G 博客地址: [CF930E]/[CF944G]Coins Exhibition - skyl ...