其他分享
首页 > 其他分享> > LeetCode #1380. Lucky Numbers in a Matrix

LeetCode #1380. Lucky Numbers in a Matrix

作者:互联网

题目

1380. Lucky Numbers in a Matrix


解题方法

遍历矩阵,将每一行的最小值存入字典,键为行下标,值为列下标。然后遍历字典的键,再遍历它所在的列,如果不是最小值就break当前循环,否则循环结束后把这个lucky number加到返回值rat中。
时间复杂度:O(mn)
空间复杂度:O(m)


代码

class Solution:
    def luckyNumbers (self, matrix: List[List[int]]) -> List[int]:
        dic = {}
        for i in range(len(matrix)):
            tempmin = matrix[i][0]
            dic[i] = 0
            for j in range(1, len(matrix[i])):
                if matrix[i][j] < tempmin:
                    tempmin = matrix[i][j]
                    dic[i] = j
        
        rat = []
        for key in dic.keys():
            for i in range(len(matrix)):
                if matrix[i][dic[key]] > matrix[key][dic[key]]:
                    break
            else:
                rat.append(matrix[key][dic[key]])
        return rat

标签:matrix,List,dic,tempmin,rat,key,1380,LeetCode,Matrix
来源: https://www.cnblogs.com/RatsCommander/p/14048517.html