Leetcode刷题笔记-不知道怎么分类的题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Sengo_GWU/article/details/82670471

36. Valid Sudoku

 

class Solution(object):
    def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
        return self.isRowValid(board) and self.isColValid(board) and self.isBoxValid(board)
    
    def isValidUnit(self, unit):
        unit = [x for x in unit if x != '.']
        return len(unit) == len(set(unit))
    
    def isRowValid(self, board):
        for r in board:
            if not self.isValidUnit(r):
                return False
        return True
    
    def isColValid(self, board):
        for c in zip(*board):
            if not self.isValidUnit(c):
                return False
        return True
    
    def isBoxValid(self, board):
        for i in (0, 3, 6):
            for j in (0, 3, 6):
                box = [board[r][c] for r in xrange(i, i+3) for c in xrange(j, j+3)]
                if not self.isValidUnit(box):
                    return False
        return True

猜你喜欢

转载自blog.csdn.net/Sengo_GWU/article/details/82670471
今日推荐