Valid Sudoku
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.
Subscribe to see which companies asked this question
[分析]
每行,每列,每个square,都不能重复.用 SET 记录即可.
[CODE]
class Solution(object): def isValidSudoku(self, board): """ :type board: List[List[str]] :rtype: bool """ row,col,square = [],[],[] for i in range(10): row.append(set()) col.append(set()) square.append(set()) for i in range(len(board)): for j in range(len(board)): n = board[i][j] if n=='.':continue elif n<'0' or n>'9':return False elif n in row[i] or n in col[j] or n in square[i/3*3+j/3]: return False else: row[i].add(n) col[j].add(n) square[i/3*3+j/3].add(n) return True
每天一小步,人生一大步!Good luck~