Java O(m+n) solution


  • 0
    M
    
        public boolean searchMatrix(int[][] matrix, int target) {
            if(matrix==null || matrix.length==0 || matrix[0].length==0) return false;
            final int M = matrix.length, N = matrix[0].length;
            int i=0, j=N-1;
            while(j>=0 && i<M){
                if(target>matrix[i][j]){
                    i++;
                } else if(target<matrix[i][j]){
                    j--;
                } else {
                    return true;
                }
            }
            return false;
        }
    
    

Log in to reply
 

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