https://leetcode.com/problems/search-a-2d-matrix-ii/
参考http://bookshadow.com/weblog/2015/07/23/leetcode-search-2d-matrix-ii/
思路1
从矩阵的右上角开始搜索
外层循环枚举行,内层枚举列
O(m + n)复杂度,要好好看!!!!!,因为每一列is ascending order from top to bottom.
class Solution:
# @param {integer[][]} matrix
# @param {integer} target
# @return {boolean}
def searchMatrix(self, matrix, target):
y = len(matrix[0]) - 1
for x in range(len(matrix)):
while y and matrix[x][y] > target:
y -= 1
if matrix[x][y] == target:
return True
return False
思路2
外层循环枚举行,内层二分搜索列