-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRowColMatrix.java
More file actions
37 lines (27 loc) · 898 Bytes
/
RowColMatrix.java
File metadata and controls
37 lines (27 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.Arrays;
public class RowColMatrix {
public static void main(String[] args) {
int[][] matrix={{1,4,7,11,15},
{ 2,5,8,12,19},
{3,6,9,16,22},
{10,13,14,17,24},
{18,21,23,26,30}
};
int target=100;
int[] index=Search2dBS(matrix,target);
System.out.println(Arrays.toString(index));
}
static int[] Search2dBS(int[][] matrix, int target) {
int i=0;//row
int j=matrix[i].length-1;
while(i<matrix.length && j>=0){
if(matrix[i][j]<target)
i++;//row++
else if(matrix[i][j]>target){
j--;//col--
}else{
return new int[]{i,j};
}
}return new int[] {-1,-1};
}
}