旋转数组的最小数字
2015-09-28 21:49
465 查看
查找和排序
题目描述把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减序列的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
注意:
1.当数组为空的时候,测试用例需要返回0。
2.当index1、index2和indexMid相等的情况下,不得不采取顺序查找的方法。
public class Solution{ public int minNumberInRotateArray(int [] array) { if(array.length<=0) return 0; int index1=0; int index2=array.length-1; int indexMid=index1; while(array[index1]>=array[index2]){ if(index2-index1==1){ indexMid=index2; break; } indexMid=(index1+index2)/2; if(array[index1]==array[index2]&&array[indexMid]==array[index1]){ return minNumberInArray(array,index1,index2); } if(array[indexMid]>=array[index1]){ index1=indexMid; } else if(array[indexMid]<=array[index2]){ index2=indexMid; } } return array[indexMid]; } public int minNumberInArray(int [] array,int index1,int index2){ int result=array[index1]; for(int i=index1+1;i<=index2;i++){ if(result>array[i]){ result=array[i]; } } return result; } public static void main(String[] args) { Solutiona=new Solution(); int [] arr={1,2,3,1}; System.out.println(a.minNumberInRotateArray(arr)); } }
相关文章推荐
- 【static&extern】用GCC输出带源代码的汇编程序进行链接的调试
- Codeforces Round #322 (Div. 2) B. Luxurious Houses 水题
- Python3+Opencv3的安装
- 浅析插入排序
- c# 加密与解密
- C基本小程序
- 属性选择器设置表单样式
- 雷军成功七字真诀:专注、极致、口碑和快(做精品的同时,还强调性价比,做自己的极限,对用户的需求最快速响应,而不是急着出产品)
- 桥接模式
- Codefoeces 581B Luxurious Houses
- Kali虚拟机及VMware Tools安装
- hdu 5486 Difference of Clustering
- CF#322-D. Three Logos-模拟水题
- HDU 5492 Find a path(数学 + DP)
- codefoces 581C Developing Skills(贪心)
- 如何A掉未来程序改
- Codefoeces 581B Luxurious Houses
- Java基础知识强化之IO流笔记09:File类功能
- C#调用外部exe
- MySql相关及如何删除MySql服务