解题思路:
首先,我们选择查找数子7为例来一步步分析查找的过程。
然后,我们选取数组右上角的9。
代码实现:
package array;
public class QuencyArray {
public static boolean FindArray(int[][] arr,int number){
int rows = arr.length;
int columns = arr[0].length;
boolean flag = false;
if(arr!=null && rows>0 && columns>0){
int row = 0;
int col = columns-1;
while(row<rows && col>0){
if(arr[row][col]<number){//说明待查找的数在下方
row++;
}else if(arr[row][col]>number){//说明待查找的数在左方
col--;
}else {
flag = true;
break;
}
}
}
return flag;
}
public static void main(String[] args) {
int[][] arr = {
{1,2,8,9},
{2,4,9,12},
{4,7,10,13},
{6,8,11,15},
{8,10,14,17}
};
System.out.println(FindArray(arr,8));//true
System.out.println(FindArray(arr,22));//false
}
}
第一次查找我们选取的是数组右上角进行查询,同样我们可以选取数组的左下角。
原文地址:http://blog.csdn.net/scgaliguodong123_/article/details/46461575