lintcode 38. 搜索二维矩阵 II

2018-09-07  本文已影响16人  cuizixin

难度:

1. Description

38. 搜索二维矩阵 II

2. Solution

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
上一篇下一篇

猜你喜欢

热点阅读