-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortedMatrix.java
62 lines (51 loc) · 1.37 KB
/
sortedMatrix.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package practice;
public class sortedMatrix {
public static void main(String[] args) {
}
public static int[] binarySearch(int[][] matrix , int row, int cStart, int cEnd, int target){
while(cStart<cEnd){
int mid = cStart+(cEnd-cStart)/2;
if(matrix[row][mid]==target){
return new int[]{row,mid};
}
else if(matrix[row][mid]>target){
cEnd=mid-1;
}
else //matrix[row][col]<target
{
cStart=mid+1;
}
}
return new int[] {-1,-1};
}
public static int[] sorted(int[][] matrix , int target)
{
int row, col;
row = matrix.length;
if(matrix.length==0){
return new int[] {-1,-1};
}
else{
col=matrix[0].length;
}
if(row==1){
binarySearch(matrix,0,0,col,target);
}
int rStart=0;
int rEnd=row-1;
int cmid= col/2;
while (rStart<(rEnd-1)){
int mid=rStart+ (rEnd-rStart)/2;
if(matrix[mid][cmid]==target){
return new int[]{cmid,mid};
}
else if(matrix[mid][cmid]>target){
rEnd=mid;
}
else{
rStart=mid;
}
}
return new int[] {-1,-1};
}
}