Java solution 0 ms


  • -2
    public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix[0][0] > target || matrix[matrix.length - 1][matrix[0].length - 1] < target) return false;
        int i;
        for(i = 0; i < matrix.length; i++) 
            if(target < matrix[i][0]) break;
        i--;
        if(matrix[i][matrix[0].length - 1] < target) return false;
        for(int j = 0; j < matrix[0].length; j++) if(matrix[i][j] == target) return true;
        return false;
    }

Log in to reply
 

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