-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path36. Valid Sudoku.py
50 lines (46 loc) · 1.66 KB
/
36. Valid Sudoku.py
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
49
50
# -*- coding: utf-8 -*-
# @Time : 2019/2/27 14:38
# @Author : xulzee
# @Email : [email protected]
# @File : 36. Valid Sudoku.py
# @Software: PyCharm
from typing import List
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
row = [{}, {}, {}, {}, {}, {}, {}, {}, {}]
column = [{}, {}, {}, {}, {}, {}, {}, {}, {}]
box = [{}, {}, {}, {}, {}, {}, {}, {}, {}]
for i in range(9):
for j in range(9):
if board[i][j] == '.':
continue
# row
if board[i][j] in row[i]:
return False
else:
row[i][board[i][j]] = 1
# column
if board[i][j] in column[j]:
return False
else:
column[j][board[i][j]] = 1
# box
box_index = (i // 3) * 3 + j // 3
if board[i][j] in box[box_index]:
return False
else:
box[box_index][board[i][j]] = 1
return True
if __name__ == '__main__':
A = [
["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"]
]
print(Solution().isValidSudoku(A))