劍指offer——面試題3,二維數組中的查找

package offer;

public class Problem03 {

	public static boolean findTheNumber(int num, int[][] m) {
		if (m == null || m.length < 0 || m[0].length < 0) {
			return false;
		}
		int row = 0;
		int col = m[0].length - 1;
		int temp;
		while (row < m.length && col > -1) {
			temp = m[row][col];
			if (temp == num) {
				return true;
			} else if (temp < num) {
				row++;
			} else if (temp > num) {
				col--;
			}
		}
		return false;
	}

	public static void main(String[] args) {
		int maritx[][] = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
		System.out.println(findTheNumber(17, maritx));
	}
}
相關文章
相關標籤/搜索