题目描述
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
解答
(一定要防止 [[]] 报错)
public class Solution {
public boolean Find(int target, int [][] array) {
int line = array.length;
if(line == 0){
return false;
}
int col = array[0].length;
if(col == 0){
return false;
}
if(array[line - 1][col - 1] < target || array[0][0] > target){
return false;
}
int l = 0 ,c = col-1;
while(l >= 0 && l < line && c >= 0 && c <= col){
if(array[l][c] > target){
c--;
}else if(array[l][c] < target){
l++;
}else{
return true;
}
}
return false;
}
}