LintCode Q389 Valid Sudoku one pass in Python

  • Jinhai ZHOU
  • 3 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
class Solution:
# @param board, a 9x9 2D array
# @return a boolean
def isValidSudoku(self, board):
visite_row = [set() for i in xrange(9)]
visite_column = [set() for i in xrange(9)]
visite_grid = [set() for i in xrange(9)]
# check row
for i in xrange(9):
for j in xrange(9):
if board[i][j] == '.':
continue
if board[i][j] in visite_row[i]:
return False
if board[i][j] in visite_column[j]:
return False
if board[i][j] in visite_grid[(i/3)*3+j/3]:
return False
else:
visite_row[i].add(board[i][j])
visite_column[j].add(board[i][j])
visite_grid[i/3*3+j/3].add(board[i][j])
return True
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。