力扣数据结构入门第5天打卡

36有效数独

   def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
        row = [set() for _ in range(9)]
        col = [set() for _ in range(9)]
        palace = [[set() for _ in range(3)] for _ in range(3)]
        for i in range(0,9):
            for j in range(0,9):
                temp=board[i][j]
                if temp==".":
                    continue
                if temp in row[i] or temp in col[j] or  temp in palace[i//3][j//3]:
                    return False
                row[i].add(temp)
                col[j].add(temp)
                palace[i//3][j//3].add(temp)

        return True

73矩阵置零

    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: None Do not return anything, modify matrix in-place instead.
        """
        hang=[]
        lie=[]
        for i in range(len(matrix)):
            for j in range(len(matrix[0])):
                if matrix[i][j]==0:
                    if i not in hang:
                        hang.append(i)
                    if j not in lie:
                        lie.append(j)
        for i in hang:
            for _ in range(len(matrix[0])):
                 matrix[i][_]=0 
        for j in lie:
            for _ in range(len(matrix)):
                 matrix[_][j]=0

上一篇:Apple列出235个与macOS Catalina不兼容的应用


下一篇:Codeforces GYM 100876 J - Buying roads 题解