lintcode 38. 搜索二维矩阵 II

难度:

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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容