Leetcode_154_Find Minimum in Rotated Sorted Array
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43416613
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.
思路:
(1)题意为给定一个已经排好序的整形数组,数组的前m(m>0)个元素被移到数组的后面形成新的数组,求新数组中的最小元素。
(2)该题考查的是数组遍历问题。该题同样比较简单,解法一属于“简单暴力”型,直接使用类库中的方法进行排序,取得最小元素(下方算法附有Arrays.sort()中的排序算法实现,感兴趣可以看看,感觉写的好复杂);解法二属于“一般解法”,通过从前、从后遍历求得最小值,从后往前的效率要高于从前往后;解法三应该是属于"高效性",不过这里尚未给出实现,猜测的思路是通过类似“二分查找”的方式来寻求最小值,感兴趣的可以研究下。
(3)希望本文对你有所帮助。
算法代码实现如下:
/**
* @author liqq
* 解法一:简单暴力
*/
public int findMin(int[] num) {
if (num == null || num.length == 0)
return 0;
Arrays.sort(num);
return num[0];
}
/**
*
* @author liqq 附加Arrays.sort() 源码 以供参考
*/
public int findMin(int[] num) {
if (num == null || num.length == 0)
return 0;
sort(num, 0, num.length);
return num[0];
}
private static void sort(int x[], int off, int len) {
// Insertion sort on smallest arrays
if (len < 7) {
for (int i = off; i < len + off; i++)
for (int j = i; j > off && x[j - 1] > x[j]; j--)
swap(x, j, j - 1);
return;
}
// Choose a partition element, v
int m = off + (len >> 1); // Small arrays, middle element
if (len > 7) {
int l = off;
int n = off + len - 1;
if (len > 40) { // Big arrays, pseudomedian of 9
int s = len / 8;
l = med3(x, l, l + s, l + 2 * s);
m = med3(x, m - s, m, m + s);
n = med3(x, n - 2 * s, n - s, n);
}
m = med3(x, l, m, n); // Mid-size, med of 3
}
int v = x[m];
// Establish Invariant: v* (<v)* (>v)* v*
int a = off, b = a, c = off + len - 1, d = c;
while (true) {
while (b <= c && x[b] <= v) {
if (x[b] == v)
swap(x, a++, b);
b++;
}
while (c >= b && x[c] >= v) {
if (x[c] == v)
swap(x, c, d--);
c--;
}
if (b > c)
break;
swap(x, b++, c--);
}
// Swap partition elements back to middle
int s, n = off + len;
s = Math.min(a - off, b - a);
vecswap(x, off, b - s, s);
s = Math.min(d - c, n - d - 1);
vecswap(x, b, n - s, s);
// Recursively sort non-partition-elements
if ((s = b - a) > 1)
sort(x, off, s);
if ((s = d - c) > 1)
sort(x, n - s, s);
}
private static void swap(int x[], int a, int b) {
int t = x[a];
x[a] = x[b];
x[b] = t;
}
private static int med3(int x[], int a, int b, int c) {
return (x[a] < x[b] ? (x[b] < x[c] ? b : x[a] < x[c] ? c : a)
: (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
}
private static void vecswap(int x[], int a, int b, int n) {
for (int i = 0; i < n; i++, a++, b++)
swap(x, a, b);
}
/**
* @author liqq
* 解法二 分别从前、从后遍历 从后遍历效率稍微高一些
*/
public int findMinFromHead(int[] num) {
if (num == null || num.length == 0)
return 0;
int len = num.length;
int leftHalfMin = num[0];
for (int i = 1; i < len; i++) {
if (leftHalfMin >= num[i]) {
leftHalfMin = num[i];
}
}
return leftHalfMin;
}
public int findMinFromEnd(int[] num) {
if (num == null || num.length == 0)
return 0;
if (num.length == 1)
return num[0];
int len = num.length;
int min = num[len - 1];
for (int i = len - 2; i >= 0; i--) {
if (min <= num[i]) {
return min;
} else {
min = num[i];
if (i == 0) {
return min;
}
}
}
return -1;
}
Leetcode_154_Find Minimum in Rotated Sorted Array的更多相关文章
- [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 I&&II
题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 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 ...
- 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 就是找到第一个违反升序的值,就 ...
- Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- leetcode 154. Find Minimum in Rotated Sorted Array II --------- java
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- 154 Find Minimum in Rotated Sorted Array II
多写限制条件可以加快调试速度. ======= Follow up for "Find Minimum in Rotated Sorted Array":What if dupli ...
随机推荐
- ToolBar控件详解
ToolBar控件详解 在Activity中添加ToolBar 1.添加库 dependencies { ... compile "com.android.support:appcompat ...
- Python 函数参数*expression 之后为什么只能跟关键字参数
python 为何要设计这种? 正确: def f(a=2,b=2,c=3): return a+b+c print(f(*(1,1),c=1)) 错误: def f(a=2,b=2,c=3): re ...
- MyEclipse如何全局搜索
1全局搜索的启动方式 CTRL+H 2全局搜索自己选择搜索方式 自己选择要搜索的东西,简单吧,里面还有很多好玩的东西需要你去发现,加油! [正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之 ...
- GDAL 2.0版本RPC校正速度测试
GDAL2.0版本的更新日志中提到了对RPC校正的优化,今天测试了一下,发现提升的速度还是蛮快的,测试的数据是一个IRS-P5的数据. 单线程测试 首先使用一个线程进行测试,使用下面的批处理进行运行, ...
- 2.QLabel,QPushButton,QLineEdit,QComboBox,QCheckBox,QRadioButton,QTextEdit,QTextBrowser,QGroupBox,QSl
1.新建一个空项目(其它项目->空QT项目): 2 添加新文件(选择C++Class) MyWidget.h #ifndef MYWIDGET_H #define MYWIDGET_H ...
- UE4读取scv文件 -- 数据驱动游戏性元素
官方文档链接:http://docs.unrealengine.com/latest/CHN/Gameplay/DataDriven/index.html 略懒,稍微麻烦重复的工作,总希望能找人帮忙一 ...
- lager_transform未定义错误
lager_transform未定义错误rebar编译时报错:D:\server\six>d:/tools/rebar/rebar.cmd compile==> mysql (compil ...
- PGM:概率图模型Graphical Model
http://blog.csdn.net/pipisorry/article/details/51461878 概率图模型Graphical Models简介 完全通过代数计算来对更加复杂的模型进行建 ...
- Struts2处理流程性需求的一种解决方案
在应用程序设计中,经常出现如下的需求. 查看用户填写的数据,而且数据是分页填写. 看下面这个情况 用户的信息有三页,分别是Form abc. 现在的问题是,后面的逻辑该如何设计. 如果把,FormAB ...
- [java面试]宇信易诚 广州分公司 java笔试题目回忆录
本文地址:http://blog.csdn.net/sushengmiyan/article/details/28479895 作者:sushengmiyan -------------------- ...