Java recursive solution, hope it helps.


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

Log in to reply
 

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