Skip to content

Commit f1ed2bc

Browse files
authored
Update Search a 2D Matrix.java
1 parent 070bec9 commit f1ed2bc

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

Medium/Search a 2D Matrix.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
class Solution {
22
public boolean searchMatrix(int[][] matrix, int target) {
3-
if (matrix.length == 0 || matrix[0].length == 0) {
4-
return false;
5-
}
63
int rowIdx = matrix.length - 1;
74
int colIdx = matrix[0].length - 1;
85
while (rowIdx >= 0 && colIdx >= 0) {
9-
int val = matrix[rowIdx][colIdx];
10-
if (val == target) {
6+
if (matrix[rowIdx][colIdx] == target) {
117
return true;
128
}
13-
if (val >= matrix[rowIdx][0]) {
9+
if (matrix[rowIdx][colIdx] > target) {
1410
colIdx--;
1511
if (colIdx < 0) {
1612
colIdx = matrix[0].length - 1;
1713
rowIdx--;
1814
}
19-
}
20-
else {
15+
} else {
2116
rowIdx--;
2217
}
2318
}

0 commit comments

Comments
 (0)