-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added solution for Programming-Problems/Search_a_2D_Matrix_II.java
- Loading branch information
Arron Stone
committed
Jul 18, 2023
1 parent
5cbf6a5
commit dba3ba8
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |