diff --git a/Programming-Problems/Search_a_2D_Matrix_II.java b/Programming-Problems/Search_a_2D_Matrix_II.java new file mode 100644 index 0000000..eb4a8cd --- /dev/null +++ b/Programming-Problems/Search_a_2D_Matrix_II.java @@ -0,0 +1,25 @@ +// 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. + +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { + return false; + } + int row = 0; + int col = matrix[0].length - 1; + + while (row < matrix.length && col >= 0) { + if (matrix[row][col] == target) { + return true; + } else if (matrix[row][col] < target) { + row++; + } else { + col--; + } + } + return false; + } +} \ No newline at end of file