LeetCode74(2021.3.30)

LC74. 搜索二维矩阵

class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int row = matrix.length, column = matrix[0].length;
        int r = 0;
        for (; r < row; ++r) {
            if (r == row - 1 || matrix[r][0] <= target && matrix[r + 1][0] > target) {
                break;
            }
        }
        int c = -1;
        for (int i = 0; i < column; ++i) {
            if (matrix[r][i] == target) {
                c = i;
                break;
            }
        }
        if (c == -1) {
            return false;
        }
        return true;
    }
}

留下评论