搜索二维矩阵 II
很简单,从左下角开始找起就行了
public class Solution087 {
public static void main(String[] args) {
int[][] matrix = 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}
};
System.out.println(Arrays.toString(searchMatrix(matrix, 23)));
}
public static int[] searchMatrix(int[][] matrix, int target) {
int m = matrix.length - 1, n = matrix[0].length - 1;
if (m <= 0 || n <= 0) return new int[]{-1, -1};
int[] ans = new int[2];
int i = m, j = 0;
while (i >= 0 && j <= n) {
if (matrix[i][j] == target) {
return new int[]{i, j};
} else if (matrix[i][j] < target) {
j++;
} else {
i--;
}
}
return new int[]{-1, -1};
}
}