题目
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits 1-9 without repetition.
- Each column must contain the digits 1-9 without repetition.
- Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition. Note:
- A Sudoku board (partially filled) could be valid but is not necessarily solvable.
- Only the filled cells need to be validated according to the mentioned rules.
解题思路
用三个字典存每行每列和九宫格里出现的数,如果出现重复,返回false
代码
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
cols = defaultdict(lambda: set())
rows = defaultdict(lambda: set())
grids = defaultdict(lambda: set())
for i in range(0,9):
for j in range(0,9):
if board[i][j] == '.':
continue
if board[i][j] in cols[j]:
return False
cols[j].add(board[i][j])
if board[i][j] in rows[i]:
return False
rows[i].add(board[i][j])
if board[i][j] in grids[10 * (i // 3) + j // 3]:
return False
grids[10 * (i // 3) + j // 3].add(board[i][j])
return True