Source of the topic:
https://leetcode.com/problems/valid-sudoku/
Test Instructions Analysis:
Judging whether a Sudoku can satisfy the rules, that is, column lines and small nine Gongge are both included and only 1 to 9.
Topic Ideas:
Just start to see this topic when think is to first to fill out the Sudoku, you can fill it is true, whether the false. But this is just a difficult problem, it is impossible to do so. Then I tested some data and found [". 87654321", "2 ...", "3 ...", "4 ...", "5 ...", "6 .....", "7 ...", "8 ..."........................., "9 ..."] this is true. Because if you want to play, then the first number to fill 1 and 9 is wrong, so he should be unsatisfied, should be false, but his answer is true.
Therefore, the topic should be judged whether the current state is not satisfied with the rules. So the value needs to be all the rows and columns of small nine judgment on the line.
Code (Python):
1 classsolution (object):2 defIsvalidsudoku (self, Board):3 """4 : Type BOARD:LIST[LIST[STR]]5 : Rtype:bool6 """7 #Column Judgment8i =09 whileI < 9:Tenj =0 OneD = {} A whileJ < 9: - ifBOARD[I][J]! ='.' andBOARD[I][J]inchD: - returnFalse the Else: -D[BOARD[I][J]] =True -J + = 1 -i + = 1 + #Line Judgment -j =0 + whileJ < 9: Ai =0 atD = {} - whileI < 9: - ifBOARD[I][J]! ='.' andBOARD[I][J]inchD: - returnFalse - Else: -D[BOARD[I][J]] =True ini + = 1 -J + = 1 to #Nine lattice +i =0 - whileI < 9: thej =0 * whileJ < 9: $m = 0;d = {}Panax Notoginseng whileM < 3: -n =0 the whileN < 3: + ifBoard[i + m][j + N]! ='.' andBoard[i + m][j + N]inchD: A returnFalse the Else: +D[board[i + M][j + N]] =True -n + = 1 $M + = 1 $J + = 3 -i + = 3 - returnTrue
View Code
Reprint Please specify source: http://www.cnblogs.com/chruny/p/4926247.html
[Leetcode] (python): 036-valid Sudoku