1. 程式人生 > >【Java】 劍指offer(10) 旋轉數組的最小數字

【Java】 劍指offer(10) 旋轉數組的最小數字

-c -i 提前 tle 更多 strong num string 測試

本文參考自《劍指offer》一書,代碼采用Java語言。

更多:《劍指Offer》Java實現合集

題目

  把一個數組最開始的若幹個元素搬到數組的末尾,我們稱之為數組的旋轉。輸入一個遞增排序的數組的一個旋轉,輸出旋轉數組的最小元素。例如數組{3, 4, 5, 1, 2}為{1, 2, 3, 4, 5}的一個旋轉,該數組的最小值為1。

思路

  數組在一定程度上是排序的,很容易分析出:可以采用二分法來尋找最小數字。

  但是這裏面有一些陷阱:

  1.遞增排序數組的本身是自己的旋轉,則最小數字是第一個數字

  2.中間數字與首尾數字大小相等,如{1,0,1,1,1,1}和{1,1,1,1,0,1},無法采用二分法,只能順序查找。

測試用例

  1.功能測試(正常旋轉數組,中間有或者無重復數字)

  2.邊界值測試(升序數組,1個數字的數組)

  3.特殊輸入測試(null,空數組)

完整Java代碼

(含測試代碼)

/**
 * 
 * @Description 旋轉數組的最小數字
 *
 * @author yongh
 * @date 2018年9月14日 下午8:30:29
 */

// 題目:把一個數組最開始的若幹個元素搬到數組的末尾,我們稱之為數組的旋轉。
// 輸入一個遞增排序的數組的一個旋轉,輸出旋轉數組的最小元素。例如數組
// {3, 4, 5, 1, 2}為{1, 2, 3, 4, 5}的一個旋轉,該數組的最小值為1。

public class MinNumberInRotatedArray {
	public int minNumberInRotateArray(int[] array) {
		if (array == null || array.length <= 0) // 空數組或null時返回0
			return 0;
		int low = 0;
		int high = array.length - 1;
		int mid = (low + high) / 2;
		//升序數組
		if (array[low] < array[high])
			return array[low];
		//中間數字與首尾數字相等
		if (array[mid] == array[high] && array[mid] == array[low]) {
			for (int i = 1; i <= high; i++) {
				if (array[i] < array[i - 1])
					return array[i];
			}
			return array[low];
		}
		//正常情況
		while (low < high) {
			if (high - low == 1)
				break;
			mid = (low + high) / 2;
			if (array[mid] <= array[high])
				high = mid;
			if (array[mid] > array[high])
				low = mid;
		}
		return array[high]; // 別錯寫成了return high; !!
	}

	// =======測試代碼======
	public void test1() {
		int[] array = null;
		System.out.println("test1:" + minNumberInRotateArray(array));
	}

	public void test2() {
		int[] array = {};
		System.out.println("test2:" + minNumberInRotateArray(array));
	}

	public void test3() {
		int[] array = { 1 };
		System.out.println("test3:" + minNumberInRotateArray(array));
	}

	public void test4() {
		int[] array = { 1, 2, 3, 4, 5, 6 };
		System.out.println("test4:" + minNumberInRotateArray(array));
	}

	public void test5() {
		int[] array = { 2, 2, 2, 2, 1, 2 };
		System.out.println("test5:" + minNumberInRotateArray(array));
	}

	public void test6() {
		int[] array = { 2, 1, 2, 2, 2, 2 };
		System.out.println("test6:" + minNumberInRotateArray(array));
	}

	public void test7() {
		int[] array = { 6, 6, 8, 9, 10, 1, 2, 2, 3, 3, 4, 5, 6 };
		System.out.println("test7:" + minNumberInRotateArray(array));
	}

	public static void main(String[] args) {
		MinNumberInRotatedArray demo = new MinNumberInRotatedArray();
		demo.test1();
		demo.test2();
		demo.test3();
		demo.test4();
		demo.test5();
		demo.test6();
		demo.test7();
	}
}

  

技術分享圖片
test1:0
test2:0
test3:1
test4:1
test5:1
test6:1
test7:1
MinNumberInRotatedArray

代碼學習

  下面的代碼是牛客網中的FINACK寫的代碼,非常簡略。在保證可讀性的情況下,希望自己也能早日寫出簡潔高效的代碼。

public class Solution {
    public int minNumberInRotateArray(int [] array) {
        int low = 0 ; int high = array.length - 1;   
        while(low < high){
            int mid = low + (high - low) / 2;        
            if(array[mid] > array[high]){
                low = mid + 1;
            }else if(array[mid] == array[high]){
                high = high - 1;
            }else{
                high = mid;
            }   
        }
        return array[low];
    }
}

  註意這段代碼的一些細節:

  1.使用low=mid+1,而不是low=mid,最終會使得low=high(即最小值位置)而跳出循環;

  2.使用high=mid,而不是high=mid-1,因為有可能mid就是最小值點,不能減1;

  3.升序數組的情況可以直接在循環中一起搞定,不用單獨列出來判斷(自己的代碼還可以改進)

  小瑕疵:

  1.該程序在array[mid] = array[high]時直接順序查找。但其實這還有可能可以用二分法的,除非還滿足array[mid] = array[low],才只能使用順序查找。所以可以先排除掉必須順序查找的情況(類似自己上面的程序,提前判斷掉),之後就可以直接刪除else if(array[mid] == array[high]){high = high - 1;這兩行了。

  2.缺少null的判斷。

收獲

  1.對於一些涉及數組的方法(如二分法等),可以用low和high,mid等來定義下標,但是,輸出時如果要求輸出數據值array[low],不要錯寫成下標low了。

  2.思維一定要考慮全面,特別是接觸到一個新的概念時,要註意到一些特例,如遞增排序數組的本身是自己的旋轉、相同數字數組等。

更多:《劍指Offer》Java實現合集

【Java】 劍指offer(10) 旋轉數組的最小數字