一个二维数组按照行按照从小到大排序,列也是如此,给定义一个数在其中查找是否存在 。
因为是有序的,我们可以选区右上角或者左上角的数进行比较。
publicboolean Find(int target, int[][] array) {
int row = 0;
int col = array[0].length-1;
while (row<=array.length-1&&col>=0){
if (target==array[row][col]){
return true;
}else if(target>array[row][col]){
row++;
}else
col--;
}
return false;
}