题解 | #二维数组中的查找#
二维数组中的查找
http://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e
从右上角(本解法)或者左下角夹逼即可
public class Solution {
public boolean Find(int target, int [][] array) {
if(array == null || array.length == 0) return false;
int row = array.length, col = array[0].length;
int i = 0, j = col-1;
while(i < row && j >= 0){
if(array[i][j] == target) return true;
else if(array[i][j] > target) j--;
else i++;
}
return false;
}
}
时间:O(m+n)即行列相加
空间:O(1)