-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path36ValidSudoku.py
54 lines (51 loc) · 2.08 KB
/
36ValidSudoku.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
51
52
53
54
# Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
# Each row must contain the digits 1-9 without repetition.
# Each column must contain the digits 1-9 without repetition.
# Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
# Note:
# A Sudoku board (partially filled) could be valid but is not necessarily solvable.
# Only the filled cells need to be validated according to the mentioned rules.
class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
# varify row
def isUniq(input):
uniq = set()
for i in input:
if i in uniq:
return False
uniq.add(i)
return True
for i in range(0, 9):
tmp = []
for j in range(0, 9):
if board[i][j] != '.':
tmp.append(board[i][j])
if not isUniq(tmp):
return False
for i in range(0, 9):
tmp = []
for j in range(0, 9):
if board[j][i] != '.':
tmp.append(board[j][i])
if not isUniq(tmp):
return False
for i in range(0, 9):
col = i % 3
row = i // 3
col *= 3
row *= 3
tmp = []
for m in range(row, row + 3):
for n in range(col, col + 3):
if board[m][n] != '.':
tmp.append(board[m][n])
if not isUniq(tmp):
return False
return True
sol = Solution()
res = sol.isValidSudoku([["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(res)