Skip to content

Commit 5dab0a8

Browse files
Create SearchIn2DMatrix.java
1 parent a0831a7 commit 5dab0a8

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Arrays/2D/SearchIn2DMatrix.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class SearchIn2DMatrix {
2+
public boolean searchMatrix(int[][] matrix, int target) {
3+
4+
int m = matrix.length;
5+
int n = matrix[0].length;
6+
7+
int l = 0;
8+
int h = (m*n)-1;
9+
10+
while(l<=h){
11+
int mid = l+(h-l)/2;
12+
int row = mid/n;
13+
int col = mid%n;
14+
15+
if(matrix[row][col] == target){
16+
return true;
17+
}
18+
else if(matrix[row][col] > target){
19+
h = mid-1;
20+
}
21+
else{
22+
l = mid+1;
23+
}
24+
}
25+
26+
return false;
27+
}
28+
}

0 commit comments

Comments
 (0)