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.
Example:
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false.
思路:
这道题目就不是那么好完全转换成一个排序数组来进行完全二分
1、根据题目规定每行是递增、每列也是递增
2、这时候画一幅图、就可以发现节点在左下角、或者右上角的时候是有序的
比如说左下角:对于他所处的这一列、它是最大值、对于他所处的这一行、它是最小值,这样子就刚好变成了部分连续
3、这样子便可以用来排除不可能的答案、
如果小于target、便证明这一列都不行、所以j++
如果大于target、便证明这一行都不行、所以i--