题解 | #二维数组中的查找#
二维数组中的查找
http://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e
java版的简单写法(8行)
从左下角/右上角开始搜索
public class Solution {
public boolean Find(int target, int [][] array) {
// 从左下角开始搜索
int cow = array.length-1; // 行
int col = 0; //列
while(cow>0 && col<array[0].length-1){
if(array[cow][col]>target) cow--;//上移
if(array[cow][col]<target) col++;//右移
if(array[cow][col]==target) return true;
}
return false;
}
}