Skip to content

Commit

Permalink
added solution for Programming-Problems/Search_a_2D_Matrix_II.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Arron Stone committed Jul 18, 2023
1 parent 5cbf6a5 commit dba3ba8
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Programming-Problems/Search_a_2D_Matrix_II.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit dba3ba8

Please sign in to comment.