An Easy Solution in Java


  • 7
    H
    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }
        int row = 0;
        int 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;
    }

  • 0

    stuck with the input matrix as [], which is here solved in the first line. However, I wonder if the input to be a row vector and target happens to lies inside that vector, the first line will skip the correct answer. Can someone help to point if my concern is too much? or the test case need to be improved and to get rid of useless []


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.