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 ...
随机推荐
- Bootstrap3 代码-变量
通过 <var> 标签标记变量. y = mx + b <var>y</var> = <var>m</var><var>x< ...
- Scheme r5rs letrec的用法
说明,这是r5rs的用法. (letrec ((<variable> <init>) ...) <body>) 假设((<variable> <i ...
- Udemy上免费的angualr2视频教程分享
福利大分享 本文作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/52592518 一晚上听了10几节课程,整体感觉很不错的 ...
- 拾遗与填坑《深度探索C++对象模型》3.3节
<深度探索C++对象模型>是一本好书,该书作者也是<C++ Primer>的作者,一位绝对的C++大师.诚然该书中也有多多少少的错误一直为人所诟病,但这仍然不妨碍称其为一本好书 ...
- springMVC源码分析--AbstractControllerUrlHandlerMapping(六)
上一篇博客springMVC源码分析--AbstractDetectingUrlHandlerMapping(五)中我们介绍了AbstractDetectingUrlHandlerMapping,其定 ...
- The packages can be overrided by Java Endorsed Standards
Endorsed Standards APIs The Endorsed Standards for Java SE constitute all classes and interfaces ...
- 巨星陨落 - Jim Gary
偶然在微软Research中搜论文时搜到了神牛Jim Gary的paper,看着照片有点眼熟,貌似在买过的哪本书中见过.于是就饶有兴致地看着Jim的生平介绍,结果- "Dr. Gray j ...
- 两种配置大数据环境的方法Ambari以及hadoop源代码安装的步骤
1.Ambari安装 Ambari & HDP(Hortonworks Data Platform) ********************************************* ...
- 使用Geolocation校正GDAL不支持的数据
对于低分数据来说,常用的校正方式就是给定数据的经纬度查找表来进行校正.在GDAL中,这种校正方式叫Geolocation array.常用的数据有国外的MODIS数据,国内的如风云系列(FY)和海洋系 ...
- 剑指Offer——网易笔试之解救小易——曼哈顿距离的典型应用
剑指Offer--网易笔试之解救小易--曼哈顿距离的典型应用 前言 首先介绍一下曼哈顿,曼哈顿是一个极为繁华的街区,高楼林立,街道纵横,从A地点到达B地点没有直线路径,必须绕道,而且至少要经C地点,走 ...