-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidSudoku.py
More file actions
40 lines (35 loc) · 1.18 KB
/
ValidSudoku.py
File metadata and controls
40 lines (35 loc) · 1.18 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
class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
# check row
for row in range(9):
if not self.checkPart(board, row, row + 1, 0, 9):
return False
# check col
for col in range(9):
if not self.checkPart(board, 0, 9, col, col + 1):
return False
# check block
for block in range(9):
row_start = (block / 3) * 3
col_start = (block % 3) * 3
if not self.checkPart(board, row_start, row_start + 3, col_start, col_start + 3):
return False
return True
def checkPart(self, board, row_start, row_end, col_start, col_end):
seen = [False] * 9
for row in range(row_start, row_end):
for col in range(col_start, col_end):
c = board[row][col]
if c == '.':
continue
else:
i = int(c) - int('1')
if seen[i]:
return False
else:
seen[i] = True
return True