Search a 2D Matrix II

Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties:

  • Integers in each row are sorted in ascending from left to right.

  • Integers in each column are sorted in ascending from top to bottom.

Solutions

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

Last updated