题解 | #二维数组中的查找#
二维数组中的查找
http://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e
[1,2,8,9],
[2,4,9,12],
[4,7,10,13],
[6,8,11,15]
从左下角往上走,比target大则往右边走,比target小则往上走
public class Solution {
public boolean Find(int target, int [][] array) {
// 行数
int row = array.length;
if (row == 0) {
return false;
}
// 列数
int col = array[0].length;
if (col == 0) {
return false;
}
int targetRow = row - 1;
int targetCol = 0;
while(targetRow >= 0 && targetCol < col) {
int value = array[targetRow][targetCol];
if (target == value) {
return true;
}
if (target > value) {
targetCol++;
} else {
targetRow--;
}
}
return false;
}
}