题解 | #二维数组查找#
二维数组中的查找
http://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e
//取右上或者左下元素
//以左下为例,左下:所在列最大,所在行最小
//target 大于他行方向移动 小于它 列方向移动
public class Solution {
public boolean Find(int target, int [][] array) {
//横向遍历
int c = array.length;
int r = array[0].length;
int i= c-1;
int j = 0;
while((0<=i)&&(i<=c-1)&(0<=j)&(j<=r-1)){
//if 等于 return true
if(target==array[i][j]){
return true;
}
//if target 小于 [i,j](左下:所在列最大,所在行最小),需要i++,列方向上移
if(target<array[i][j]){
i--;
continue;
}
if(target>array[i][j]){
j++;
continue;
}
}
return false;
}
}
