Find Minimum in Rotated Sorted Array
2015-08-12 15:04
274 查看
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.
比较中间元素,比末尾大,在右侧查找,否则在左侧。
头比尾小,则没有旋转,最小的是头。
(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.
比较中间元素,比末尾大,在右侧查找,否则在左侧。
头比尾小,则没有旋转,最小的是头。
public int findMin(int[] num) { if(num==null||num.length==0) return 0; return find(num,0,num.length-1); } private int find(int[] num,int low,int high){ if(low==high) return num[low]; if(high-low==1) return Math.min(num[low], num[high]); int mid=(high+low)/2; if(num[low]<num[high])//no rotated return num[low]; if(num[mid]>num[high])//go right return find(num,mid+1,high); else //go left return find(num,low,mid); }
相关文章推荐
- hdu 1711 Number Sequence KMP
- 袁岚峰:中国科技实力正以多快的加速度逼近美国
- Ajax(2)使用 JavaScript 和 Ajax 发出异步请求
- js控件打印
- CentOS 恢复 rm -rf * 误删数据
- PAT 1052. Linked List Sorting (25)
- IOS各种传值方式
- yum命令指南-yum使用方法
- ubuntu下ltib安装问题(解决)
- 十六进制颜色值和ARGB颜色值的转换
- 点击tabBar跳转
- 欢迎使用CSDN-markdown编辑器
- 关于用dom4j生成xml后第二行空行的问题
- HashMap,LinkedHashMap,TreeMap的有序性
- VCPP/CPP_表格控件-Ultimate Grid
- Java多线程系列--“JUC锁”06之 Condition条件
- jsoup使用例子(仅作参考)
- SQL 的case when 的用法
- 20条Linux命令面试问答
- Android项目Tab类型主界面大总结 Fragment+TabPageIndicator+ViewPager