LintCode Q389 Valid Sudoku in Python

  • Jinhai ZHOU
  • 6 Minutes
  • 2016年12月9日
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
class Solution:
# @param board, a 9x9 2D array
# @return a boolean
def isValidSudoku(self, board):
# check row
for i in xrange(9):
visited = [False]*9
for j in xrange(9):
if board[i][j] == '.':
pass
elif visited[int(board[i][j]) - 1]:
return False
else:
visited[int(board[i][j]) - 1] = True
# check column
for i in xrange(9):
visited = [False]*9
for j in xrange(9):
if board[j][i] == '.':
pass
elif visited[int(board[j][i]) - 1]:
return False
else:
visited[int(board[j][i]) - 1] = True
# check grid
for i in xrange(0, 9, 3):
for j in xrange(0, 9, 3):
visited = [False]*9
for k in xrange(0, 9):
if board[i + k/3][j + k % 3] == '.':
pass
elif visited[int(board[i + k/3][j + k % 3]) - 1]:
return False
else:
visited[int(board[i + k/3][j + k % 3]) - 1] = True
return True
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。