【力扣7】搜索二维矩阵

Posted by Hilda on September 13, 2025

74. 搜索二维矩阵

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0) return false;
        int row = matrix.length - 1;// 行最大索引
        int col = matrix[0].length - 1 ; // 列最大索引
        int i = row;
        int j = 0;
        while (i >= 0 && j <= col) {
            int tmp = matrix[i][j];
            if (tmp == target) {
                return true;
            } else if (tmp < target) {
                j++;
            } else {
                i--;
            }
        }
        return false;
    }
}