36. Valid Sudoku*

来源:互联网 发布:ios手绘软件 编辑:程序博客网 时间:2024/06/06 04:23

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.


A partially filled sudoku which is valid.

Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

Reference

class Solution(object):    def isValidSudoku(self, board):        """        :type board: List[List[str]]        :rtype: bool        """        used1 = [([0] * 9) for i in range(9)]        used2 = [([0] * 9) for i in range(9)]        used3 = [([0] * 9) for i in range(9)]                for i in range(9):            for j in range(9):                if board[i][j]!='.':                    num = int(board[i][j]) - 1                    k = i / 3 * 3 + j / 3                    if used1[i][num] or used2[j][num] or used3[k][num]:                        return False                    used1[i][num] = 1                    used2[j][num] = 1                    used3[k][num] = 1        return True



0 0
原创粉丝点击