剑指offer 8. 旋转数组的最小数字
来源:互联网 发布:cad2014软件多少钱 编辑:程序博客网 时间:2024/06/05 04:00
// 题目:输入一个旋转数组,输出其中的最小值// 解法:使用二分法进行查找public class Main {public static void main(String[] args) {System.out.println(findMinNum(new int[]{1,0,1,1,1}));}public static int findMinNum(int[] input){int low = 0;int high = input.length-1;int mid = low;while(input[low]>=input[high]){if(high-low == 1){//如果high-low = 1,则返回high的值,因为high的值一定小于等于low的mid = high;break;}mid = (low+high)/2;if(input[low] == input[high] && input[mid] == input[low]){//如果出现low,mid,high都相等,则只能顺序查找return minInOrder(input, low, high);}if(input[mid]>=input[low]){//如果mid>=low则最小值在mid和high之间low = mid;}if(input[mid]<=input[high]){//如果mid<high则最小值在low和mid之间high = mid;}}return input[mid];}public static int minInOrder(int[] input, int low, int high){//顺序查找函数int result = input[low];for(int i = low+1;i<=high;i++){if(input[i]<result){result = input[i];}}return result;}}
阅读全文
0 0
- 【剑指offer】旋转数组的最小数字
- 剑指offer--旋转数组的最小数字
- 剑指offer--旋转数组的最小数字
- 剑指offer:旋转数组的最小数字
- 《剑指offer》旋转数组的最小数字
- 剑指 offer:旋转数组的最小数字
- 剑指offer-旋转数组的最小数字
- 剑指offer 旋转数组的最小数字
- 【剑指offer】旋转数组的最小数字
- 剑指offer 旋转数组的最小数字
- 剑指offer-旋转数组的最小数字
- 剑指Offer:旋转数组的最小数字
- 剑指Offer 旋转数组的最小数字
- 剑指offer|旋转数组的最小数字
- 剑指offer:旋转数组的最小数字
- 【剑指offer】旋转数组的最小数字
- 《剑指offer》旋转数组的最小数字
- 剑指Offer:旋转数组的最小数字
- 如何传输文件到Linux服务器上
- 字符串中单词的逆转
- Educational Codeforces Round 21 808A Lucky Year
- hook钩子的使用
- install pstricks into texlive
- 剑指offer 8. 旋转数组的最小数字
- 21.输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判
- 打造高质效的技术团队 —— 混乱篇
- I2S音频总线(一)————数字音频技术
- Machine Learning Pipelines for R
- CentOS7 安装Maven
- JDBC 中preparedStatement和Statement区别
- Educational Codeforces Round 21 808B Average Sleep Time
- 12. Integer to Roman LeetCode题解