美文网首页
lintcode 38. 搜索二维矩阵 II

lintcode 38. 搜索二维矩阵 II

作者: cuizixin | 来源:发表于2018-09-07 01:43 被阅读16次

    难度:

    1. Description

    38. 搜索二维矩阵 II

    2. Solution

    • python
    class Solution:
        """
        @param matrix: A list of lists of integers
        @param target: An integer you want to search in matrix
        @return: An integer indicate the total occurrence of target in the given matrix
        """
        def searchMatrix(self, matrix, target):
            # write your code here
            if len(matrix)==0:
                return 0
            if len(matrix[0])==0:
                return 0
            m = len(matrix)
            n = len(matrix[0])
            cnt = 0
            for i in range(n):
                if target<matrix[0][i]:
                    break
                if target>matrix[m-1][i]:
                    continue
                for j in range(m):
                    if target == matrix[j][i]:
                        cnt+=1
            return cnt
    

    3. Reference

    1. https://www.lintcode.com/problem/search-a-2d-matrix-ii/description?_from=ladder

    相关文章

      网友评论

          本文标题:lintcode 38. 搜索二维矩阵 II

          本文链接:https://www.haomeiwen.com/subject/pzkqgftx.html