[Leetcode] (python): 036-valid Sudoku

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.