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 ...
随机推荐
- Android简易实战教程--第四十九话《满屏拖动的控件》
今天做个有意思的效果吧,控件的拖拽,简单实用,逻辑清晰点3分钟看完. 说的很高大上,其实就是拖动Button按钮跟着鼠标位置满手机屏幕跑罢了. 直接上简单的代码吧: public class Main ...
- 有一个排序二叉树,数据类型是int型,如何找出中间大的元素。
void tree2Dll(TNode* root, TNode*& tail) { if (!root) { return; } if (root->left) { tree2Dll( ...
- 让你的代码量减少3倍!使用kotlin开发Android(二) --秘笈!扩展函数
本文承接上一篇文章:让你的代码量减少3倍!使用kotlin开发Android(一) 创建Kotlin工程 本文同步自博主的私人博客wing的地方酒馆 上一节说到,kotlin可以省去getter,se ...
- java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener问题解决
今天搭建SSH项目的时候出现了如下错误: 严重: Error configuring application listener of class org.springframework.web.con ...
- SceneKit一个3D场景角色的代码重构
SuperSpaceMan3D是一个以SceneKit为基础的小游戏项目,作者展示了用SceneKit开发3D游戏的强大威力.不过在实际运行时会发现有一些小bug,这里我们依次尝试将其修复 首先,当s ...
- Dynamics CRM 权限整理二
接上篇http://blog.csdn.net/vic0228/article/details/50510605,继续列举CRM相关权限 prvReadBusinessUnit privilege(I ...
- mysql进阶(二十七)数据库索引原理
mysql进阶(二十七)数据库索引原理 前言 本文主要是阐述MySQL索引机制,主要是说明存储引擎Innodb. 第一部分主要从数据结构及算法理论层面讨论MySQL数据库索引的数理基础. ...
- Socket实现单客户端与服务器对话功能
单客户端,顾名思义,就是客户端只有一个用户去访问服务器,然后服务器根据该客户请求返回信息,先看下效果图: 服务端(左)和客户端(右): 注意,我是用了两个eclipse,一个只放服务端文件,一个只放客 ...
- [ExtJS5学习笔记]第二十八节 sencha ext js 5.1.0发布版本正式发布 extjs doc下载地址
本文地址:http://blog.csdn.net/sushengmiyan/article/details/41911539 本文作者:sushengmiyan ------------------ ...
- [ExtJS5学习笔记]第二十七节 CMD打包错误 Error C2009: YUI Parse Error (identifier is a reserved word => debugger;)
本文地址:http://blog.csdn.net/sushengmiyan/article/details/41242993 本文作者:sushengmiyan ------------------ ...