【Java】 劍指offer(3) 二維數組中的查找

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

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

題目

  在一個二維數組中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成一個函數,輸入這樣的一個二維數組和一個整數,判斷數組中是否含有該整數。數組

思路

  查找整數時,若是從左上角開始查找,狀況較爲複雜,能夠轉換思路,從右上角開始查找:左邊數字比較小,右邊數字比較大,容易進行判斷。ide

測試用例函數

  1.要查找的數字在數組中post

  2.要查找的數字不在數組中測試

  3.數組爲空url

  4.數組不知足大小規則spa

  5.數組每行長度不一致.code

完整Java代碼

(含測試代碼)

/**
 * @Description 二維數組中的查找 
 *
 * @author yongh
 * @date 2018年7月16日 下午2:20:59
 */

// 題目:在一個二維數組中,每一行都按照從左到右遞增的順序排序,每一列都按
// 照從上到下遞增的順序排序。請完成一個函數,輸入這樣的一個二維數組和一個
// 整數,判斷數組中是否含有該整數。

public class FindInPartiallySortedMatrix {

	/*
	 * 判斷二維數組matrix中是否含有整數a
	 * 返回值爲a的下標,{-1,-1}表明不存在
	 */
	public int[] find(int[][] matrix, int a) {
		int[] index = { -1, -1 };

		// 判斷數組是否正確
		if (matrix == null || matrix.length <= 0) {
			System.out.println("數組無效!");
			return index;
		}
		// 判斷數組數字的大小是否符合大小規則
		int columns = matrix[0].length;
		for (int i = 0; i < matrix.length; i++) {
			if (matrix[i].length != columns) {
				System.out.println("數組列數不一致!");
				return index;
			}
			for (int j = 0; j < matrix[i].length; j++) {
				if (i == 0 && j == 0)
					// matrix[0][0]不比較
					break;
				if (i == 0) { // 第一行的數,僅和前一列的數比較
					if (matrix[i][j] < matrix[i][j - 1]) {
						System.out.println("數組中數字大小不符合要求!");
						return index;
					}
				} else if (j == 0) { // 第一列的數,僅和前一行的數比較
					if (matrix[i][j] < matrix[i - 1][j]) {
						System.out.println("數組中數字大小不符合要求!");
						return index;
					}
				} else if (matrix[i][j] < matrix[i - 1][j] || matrix[i][j] < matrix[i][j - 1]) {
					// 其他位置的數字,和前一行或前一列的比較
					System.out.println("數組中數字大小不符合要求!");
					return index;
				}
			}
		}

		// 正式查找
		int row = 0; // 行數
		int column = matrix[0].length - 1; // 列數
		while (row <= matrix.length - 1 && column >= 0) {
			if (a == matrix[row][column]) {
				index[0] = row;
				index[1] = column;
				System.out.println("數字" + a + "在二維數組中的下標爲:" + index[0] + "," + index[1]); // 注意下標是從0開始的
				return index;
			} else if (a < matrix[row][column]) {
				column--;
			} else {
				row++;
			}
		}
		System.out.println("數組中不含數字:" + a);
		return index;
	}

	// ==================================測試代碼==================================

	// 1 2 8 9
	// 2 4 9 12
	// 4 7 10 13
	// 6 8 11 15
	// 要查找的數在數組中
	public void test1() {
		System.out.print("test1:");
		int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
		int[] index = find(matrix, 7);
	}

	// 1 2 8 9
	// 2 4 9 12
	// 4 7 10 13
	// 6 8 11 15
	// 要查找的數不在數組中
	public void test2() {
		System.out.print("test2:");
		int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
		int[] index = find(matrix, 5);
	}

	// 數組爲空
	public void test3() {
		System.out.print("test3:");
		int[][] matrix = null;
		int[] index = find(matrix, 7);
	}

	// 1 2 8 9
	// 4 3 9 12
	// 4 7 10 13
	// 6 8 11 15
	// 數組不知足大小規則
	public void test4() {
		System.out.print("test4:");
		int[][] matrix = { { 1, 2, 8, 9 }, { 4, 3, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
		int[] index = find(matrix, 7);
	}

	// 數組每行長度不一致
	public void test5() {
		System.out.print("test5:");
		int[][] matrix = { { 1, 2, 8 }, { 4, 3, 9, 12 }, { 4, 7, 10 }, { 6, 8, 11, 15 } };
		int[] index = find(matrix, 7);
	}

	public static void main(String[] args) {
		FindInPartiallySortedMatrix f = new FindInPartiallySortedMatrix();
		f.test1(); // 注意下標是從0開始的
		f.test2();
		f.test3();
		f.test4();
		f.test5();
	}
}

  

test1:數字7在二維數組中的下標爲:2,1
test2:數組中不含數字:5
test3:數組無效!
test4:數組中數字大小不符合要求!
test5:數組列數不一致!
FindInPartiallySortedMatrix

 

====================================================================

  上面代碼考慮了數組數字大小不符合規則的狀況,因此較爲繁瑣。

  在牛客網中提交的代碼以下(不含測試代碼):

public class Solution {
    public boolean Find(int target, int [][] array) {
        if(array==null||array.length<=0){
            return false;
        }
 
        int row=0;
        int column=array[0].length-1;
        while(row<=array.length-1 && column>=0){
            if(target==array[row][column]){
                return true;
            }else if(target<array[row][column]){
                column--;
            }else{
                row++;
            }
        }
        return false;
    }
}

  

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

相關文章
相關標籤/搜索