题目链接
法一(Z字形查找)
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0) {
return false;
}
int row = 0, col = matrix[0].length - 1;
while (row < matrix.length && col >= 0) {
if (matrix[row][col] == target) {
return true;
} else if (matrix[row][col] < target) {
row++;
} else {
col--;
}
}
return false;
}
本地测试
lay.showTitle(240);
Solution240 sol240 = new Solution240();
int[][] matrix240 = new int[][]{{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}};
arrayOpt.showIntTwoDimArray(matrix240, matrix240.length);
System.out.println(sol240.searchMatrix(matrix240, 5));