-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy path36_valid_sudoku.py
More file actions
48 lines (40 loc) · 1.31 KB
/
36_valid_sudoku.py
File metadata and controls
48 lines (40 loc) · 1.31 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
47
48
class Solution:
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
if not board or not board[0] or len(board) != len(board[0]):
return False
n = len(board)
EMPTY = '.'
CANDS = '123456789'
for x in range(n):
used = set()
for y in range(n):
if board[x][y] == EMPTY:
continue
if board[x][y] not in CANDS:
return False
if board[x][y] in used:
return False
used.add(board[x][y])
for y in range(n):
used = set()
for x in range(n):
if board[x][y] == EMPTY:
continue
if board[x][y] in used:
return False
used.add(board[x][y])
for i in range(3):
for j in range(3):
used = set()
for x in range(i * 3, i * 3 + 3):
for y in range(j * 3, j * 3 + 3):
if board[x][y] == EMPTY:
continue
if board[x][y] in used:
return False
used.add(board[x][y])
return True