-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchaDMatrix_74.java
More file actions
31 lines (29 loc) · 964 Bytes
/
Copy pathSearchaDMatrix_74.java
File metadata and controls
31 lines (29 loc) · 964 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
package Array;
/**
* 判断在二维数组中是否存在某个数,该二维数组行递增,列递增
*
*/
public class SearchaDMatrix_74 {
public static boolean searchMatrix(int[][] matrix, int target) {
if(matrix==null||matrix.length==0) return false;
int w=matrix[0].length;
int h=matrix.length;
int x=0,y=w-1;
while (y>=0&&x<=h-1){
int i=matrix[x][y];
if(i>target){
y=y-1;
}else if(i<target)
x=x+1;
else
return true;
}
return false;
}
public static void main(String[] args) {
int[][] m={{1, 3, 5, 7},{10, 11, 16, 20},{23, 30, 34, 50}};
System.out.println("target:23 "+searchMatrix(m,23));
System.out.println("target:11 "+searchMatrix(m,11));
System.out.println("target:70 "+searchMatrix(m,70));
}
}