36. Valid Sudoku
problem
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.
判断一个数独是不是符合形式规则,加数正确与否不管
那么题目转换成每行每列井字的九个方块是不是0-9不重复
solution
- 大致思路
行直接判断内层list
列zip后判断内层tuple
井字截取三行,每行截取三个元素 3*3的循环,
class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
for elem in board:
if not self.verfiy(elem):
return False
row = zip(*board)
for elem in row:
if not self.verfiy(elem):
return False
for i in range(3):
for j in range(3):
elem = []
elem.extend(board[i*3][j*3:j*3+3])
elem.extend(board[i*3+1][j*3:j*3+3])
elem.extend(board[i*3+2][j*3:j*3+3])
if not self.verfiy(elem):
return False
return True
def verfiy(self,listNum):
listNum = [x for x in listNum if x != '.']
setNum = setNum = set(['1', '2', '3', '4', '5', '6', '7', '8', '9'])
if len(listNum) == len(set(listNum)) and set(listNum) <= setNum:
return True
return False

浙公网安备 33010602011771号