码迷,mamicode.com
首页 > 其他好文 > 详细

240 Search a 2D Matrix II

时间:2015-07-24 06:55:12      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:

240 Search a 2D Matrix II 

这是个young‘s矩阵,最好的就是O(m+n)的算法了

class Solution:
    # @param {integer[][]} matrix
    # @param {integer} target
    # @return {boolean}
    def searchMatrix(self, matrix, target):
        m = len(martix)
        if m == 0:
            return False
        n = len(matrix[0])
        i, j = m - 1, 0
        while i >= 0 and j < n:
            if matrix[i][j] == target:
                return True
            elif matrix[i][j] < target:
                j += 1
            else:
                i -= 1
        return False

 

240 Search a 2D Matrix II

标签:

原文地址:http://www.cnblogs.com/dapanshe/p/4672300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!