array: 待查找的二維數組 target:查找的數字
查找到返回true,查找不到返回false
public
class
Solution {
public
boolean
Find(
int
[][] array,
int
target) {
int
m = array.length;
int
n = array[
0
].length;
if
(m ==
0
|| n ==
0
) {
return
false
;
}
if
(target < array[
0
][
0
] || target > array[m -
1
][n -
1
]) {
return
false
;
}
int
i =
0
;
int
j = n -
1
;
while
(i < m && j >=
0
) {
if
(target < array[i][j]) {
j--;
}
else
if
(target > array[i][j]) {
i++;
}
else
{
return
true
;
}
}
return
false
;
}
}