Simple to code and tricky to think solution with O(m+n)


  • 0
    A

    Here we have to start searching from top right corner move towards bottom left corner(or start from bottom left corner until reaching top right corner )

    public class Solution {
        public boolean searchMatrix(int[][] matrix, int target) {`
        int row=0;
        int col=matrix[0].length-1;
        while(col>=0&&row<matrix.length){
            if(matrix[row][col]==target)
                return true;
            if(target<matrix[row][col])
                col--;
            else
                row++;
        }
        return false;
        }
    }

Log in to reply
 

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