旋转数组中的最小数字
来源:互联网 发布:gz网络语什么意思 编辑:程序博客网 时间:2024/05/21 18:35
题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2}为
{1,2,3,4,5}的一个旋转,该数组的最小值为1。
可以使用二分查找法,时间复杂度O(logN)。
package offer;public class MinNumInRotatedArray {public static void main(String[] args) {int[] arr = {3,4,5,1,2};System.out.println(Min(arr));}public static int Min(int[] num){if(num==null || num.length==0)return Integer.MAX_VALUE;int index1 = 0;int index2 = num.length - 1;int indexMid = index1;while(num[index1]>=num[index2]){if(index2-index1==1){indexMid = index2;break;}indexMid = (index1+index2)/2;if(num[index1]==num[index2] && num[indexMid]==num[index1])return MinInOrder(num,index1,index2);if(num[indexMid]>=num[index1])index1 = indexMid;else if(num[indexMid]<=num[index2])index2 = indexMid;}return num[indexMid];}private static int MinInOrder(int[] num, int index1, int index2){int result = num[index1];for(int i=index1+1; i<=index2; i++){if(result>num[i])result = num[i];}return result;}}
0 0
- 旋转数组中的最小数字
- 旋转数组中的最小数字
- 旋转数组中的最小数字
- 旋转数组中的最小数字
- 旋转数组中的最小数字
- 旋转数组中的最小数字
- 旋转数组中的最小数字
- 旋转数组中的最小数字
- ~旋转数组中的最小数字~
- 旋转数组中的最小数字
- 旋转数组中的最小数字
- 旋转数组中的最小数字
- 剑指Offer旋转数组中的最小数字
- 05 旋转数组中的最小数字
- 【数据结构】旋转数组中的最小数字
- 【剑指offer】旋转数组中的最小数字
- 剑指Offer:旋转数组中的最小数字
- 找出旋转数组中的最小数字
- Ubuntu 14.04下安装MySQL
- 第十课 调试技术-进入函数内部去【项目1-2】
- Codeforces Round #296 (Div. 2) C. Glass Carving
- 【.NET】WebKit的安装与使用
- Python文件夹与文件的操作
- 旋转数组中的最小数字
- 计算机牛人博客和代码汇总
- JAVA启动参数整理
- Linux内核中断和异常分析(中)
- 解决条目布局中imageview个数不确定
- C++高级进阶 第二季:mutable 关键字
- NOIP 2006 tg T2金明的预算方案
- 手机参数篇
- Redhat linux7.0 网卡的安装