#-*-Coding:utf8-*-
‘‘‘
__author__ = ' [email protected] '
36:valid Sudoku
https://oj.leetcode.com/problems/valid-sudoku/
Determine if a Sudoku is valid, according To:sudoku puzzles-the Rules.
The Sudoku board could be partially filled, where empty cells is filled with the character '.
Note:
A Valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
===comments by dabay===
Check each 3x3 lattice first.
Then traverse the board, and when the number is encountered, check the row and column in which it is located. Here, the checked rows and columns are recorded with D_row and D_col, avoiding duplicate checks.
‘‘‘
Class Solution:
# @param board, a 9x9 2D array
# @return A Boolean
def isvalidsudoku (self, Board):
For I in xrange (0, 9, 3):
For J in Xrange (0, 9, 3):
D = {}
For x in xrange (i, i+3):
For y in Xrange (J, J+3):
If board[x][y] = = '. ':
Continue
n = board[x][y]
If n in D:
Return False
D[n] = True
D_row = {}
D_col = {}
For I in xrange (0, 9):
For J in Xrange (0, 9):
If board[i][j] = = '. ':
Continue
num = Board[i][j]
If I not in D_row:
D = {}
For x in xrange (0, 9):
If board[i][x] = = '. ':
Continue
n = board[i][x]
If n in D:
Return False
D[n] = True
D_row[i] = True
If J not in D_col:
D = {}
For y in xrange (0, 9):
If board[y][j] = = '. ':
Continue
n = board[y][j]
If n in D:
Return False
D[n] = True
D_COL[J] = True
Return True
def main ():
Sol = solution ()
board = [
"53..7 ...",
"6..195 ...",
". 98....6.",
"8...6...3",
"4..8.3..1",
"7...2...6",
". 6....28.",
"... 419..5 ",
".... 8..79 "
]
Print Sol.isvalidsudoku (board)
if __name__ = = ' __main__ ':
Import time
Start = Time.clock ()
Main ()
Print "%s sec"% (Time.clock ()-start)
[Leetcode] [Python]36:valid Sudoku