[Leetcode] [Python]36:valid Sudoku

Source: Internet
Author: User

#-*-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

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.