-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36_valid_sudoku.py
More file actions
46 lines (36 loc) · 1.32 KB
/
36_valid_sudoku.py
File metadata and controls
46 lines (36 loc) · 1.32 KB
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
from typing import List
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
row = [[False] * 9 for _ in range(9)]
column = [[False] * 9 for _ in range(9)]
box = [[False] * 9 for _ in range(9)]
for i in range(9):
for j in range(9):
if board[i][j] == ".":
continue
cur = ord(board[i][j]) - 49
k = i // 3 * 3 + j // 3
if row[i][cur] or column[j][cur] or box[k][cur]:
return False
row[i][cur] = True
column[j][cur] = True
box[k][cur] = True
return True
def test():
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"]
]
target = True
s = Solution()
ans = s.isValidSudoku(board)
assert ans == target
if __name__ == "__main__":
test()