zoukankan      html  css  js  c++  java
  • 剑指offer:旋转数组的最小数字

    思路

      数组在一定程度上是排序的,很容易分析出:可以采用二分法来寻找最小数字

      如果数组的旋转是其本身,则最小数字是第一个数字

    public class 旋转数组的最小数字 {
    	public int minNumberInRotateArray(int [] array) {
    		if(array.length==0){
    			return 0;
    		}
    		if(array[0]<array[array.length-1]){
    			return array[0];
    		}
    		
    		int start = 0;
    		int end = array.length-1;
    		int flag = 0;
    		//3 4 5 1 2
    		while(start+1!=end){
    			int mid = (start+end)/2;
    			//向右靠拢
    			if(array[mid]>array[start]){
    				start = mid;
    			}else if(array[mid]<array[end]){//向左靠拢
    				end = mid;
    			}else{
    				start++;
    			}
    		}
    		
    		return array[end];
    	}
    }
    

      

  • 相关阅读:
    双色球随机一注
    if else的简写
    select
    预解析
    json
    数组方法
    arguments
    国密SM4算法
    AES算法
    Feistel算法结构与DES加密算法
  • 原文地址:https://www.cnblogs.com/blzm742624643/p/12230392.html
Copyright © 2011-2022 走看看