0
点赞
收藏
分享

微信扫一扫

036.valid-sudoku

南陵王梁枫 2022-02-25 阅读 42

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

A partially filled sudoku which is valid.


Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
 

class Solution(object):
    def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
        cacheCol = [[0] * 9 for _ in xrange(0, 10)]
        cacheRow = [[0] * 9 for _ in xrange(0, 10)]
        cacheBox = [[0] * 9 for _ in xrange(0, 10)]
        
        for i in xrange(0, 9):
            for j in xrange(0, 9):
                ib = (i/3)*3 + j/3
                if board[i][j] == ".":
                    continue
                num = int(board[i][j]) - 1
                if cacheRow[i][num] != 0 or cacheCol[j][num] != 0 or cacheBox[ib][num] != 0:
                    return False
                cacheRow[i][num] = 1
                cacheCol[j][num] = 1
                cacheBox[ib][num] = 1
        return True
举报

相关推荐

0 条评论