-
Notifications
You must be signed in to change notification settings - Fork 0
/
二维数组查找
50 lines (48 loc) · 1.59 KB
/
二维数组查找
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
//错误的代码
public class Solution {
public boolean Find(int target, int [][] array) {
if(array==null) //数组为空或者长度不对时均returnfalse
return false;
int row =array.length;
int cow=array[0].length;
if(row<=0||cow<=0)
return false;
//从二维数组的第一个数组内开始查找
row=0;
cow=cow-1; //从该数组的末尾往前找
while(true){
//未查找到的时候跳出循环并返回false
if(row>=array.length||cow<0){
return false;
}
//如果查找的当前值大于目标值时候,往前移,若小于时候,往下移
if(array[row][cow]==target){
return true;
}else if(array[row][cow]>target){
cow--;
}else{
row++;
}
}
}
}
//老方法,一个一个找吧
public class Solution {
public boolean Find(int target, int [][] array) {
if(array==null)
return false;
int row =array.length;
int col=array[0].length;
if(row<=0||col<=0)
return false;
for(int i=0;i<row;i++){
if(target>=array[i][0]&&target<=array[i][col-1]){ //如果target不在该数组大小范围内,直接跳过
for(int j=0;j<col;j++){
if(target==array[i][j])
return true;
}
}
}
return false;
}
}