1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| from typing import List
class Solution: def isValidSudoku(self, board: List[List[str]]) -> bool: help = 10 for i in range(9): row = set() col = set() box = set() for j in range(9): row.add(board[i][j] if board[i][j] != '.' else help) help += 1 col.add(board[j][i] if board[j][i] != '.' else help) help += 1 box.add(board[i // 3 * 3 + j // 3][i % 3 * 3 + j % 3] if board[i // 3 * 3 + j // 3][i % 3 * 3 + j % 3] != '.' else help) help += 1
if len(row) != 9 or len(col) != 9 or len(box) != 9: return False return True
if __name__ == '__main__': solu = Solution() board = [["5", "3", ".", ".", "7", ".", ".", ".", "."], ["6", ".", ".", "1", "9", "5", ".", ".", "."], [".", "9", "8", ".", ".", ".", ".", "6", "."], ["8", ".", ".", ".", "6", ".", ".", ".", "3"], ["4", ".", ".", "8", ".", "3", ".", ".", "1"], ["7", ".", ".", ".", "2", ".", ".", ".", "6"], [".", "6", ".", ".", ".", ".", "2", "8", "."], [".", ".", ".", "4", "1", "9", ".", ".", "5"], [".", ".", ".", ".", "8", ".", ".", "7", "9"]] board = [["8", "3", ".", ".", "7", ".", ".", ".", "."], ["6", ".", ".", "1", "9", "5", ".", ".", "."], [".", "9", "8", ".", ".", ".", ".", "6", "."], ["8", ".", ".", ".", "6", ".", ".", ".", "3"], ["4", ".", ".", "8", ".", "3", ".", ".", "1"], ["7", ".", ".", ".", "2", ".", ".", ".", "6"], [".", "6", ".", ".", ".", ".", "2", "8", "."], [".", ".", ".", "4", "1", "9", ".", ".", "5"], [".", ".", ".", ".", "8", ".", ".", "7", "9"]]
res = solu.isValidSudoku(board) print(res)
|