您的位置:首页 > 其它

leetcode -- Search a 2D Matrix II -- 重点要理解

2015-12-15 21:37 253 查看
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

外层循环枚举行,内层二分搜索列
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: