Write an efficient algorithm that searches for a value in an m x n matrix. 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:
Given target = 5, return true.
Given target = 20, return false.
解法1:O(N + M)
这题和Matrix I的区别是上一行和下一行不是递增的,也就是说不能串起来当成一个sorted array来处理。
这题要变换下思路,考虑右上角和左下角两个点。比如左下角,比数字大就往上,比它数字小就往右。直到找到所要求的答案。
Java