Search a 2D Matrix II
Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
For example,
Consider the following matrix:
Giventarget=5
, returntrue
.
Giventarget=20
, returnfalse
.
Solution
设置两个指针,每一行的最后一列是非常重要的参考,如果大于这个值,就换行,如果小于这个,就一定在同一行,于是换列。
Last updated
Was this helpful?