search a 2D matrix
Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:
Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row.
For example,
Consider the following matrix:
Giventarget=3
, returntrue
.
分析
2次二分,先找column,再找row.第一次用column用0,loop row. 最后判断row <= target。第二次row用前面二分结果,loop column.
答案
Last updated
Was this helpful?