36.有效的数独

没啥说的,写了半天

class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        for line in board:
            l = list(filter(lambda x: x != ".", line))
            if len(l) != len(set(l)):
                return False
        for col in range(9):
            line = list(filter(lambda x: x != ".", [line[col] for line in board]))
            if len(line) != len(set(line)):
                return False
        for row in range(0,9,3):
            for col in range(0,9,3):
                line = [board[row][col],board[row][col+1],board[row][col+2], board[row+1][col],board[row+1][col+1],board[row+1][col+2],board[row+2][col],board[row+2][col+1],board[row+2][col+2]]
                line = list(filter(lambda x: x != ".", line))
                if len(line) != len(set(line)):
                    return False
        return True

评论