36. Valid Sudoku
Leetcode link for this question
Discription:
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.
Analyze:
Code 1:
class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
return 1 == max(collections.Counter(
x
for i, row in enumerate(board)
for j, c in enumerate(row)
if c != '.'
for x in ((c, i), (j, c), (i/3, j/3, c))
).values() + [1])
Submission Result:
Status: Accepted
Runtime: 133 ms
Ranking: beats 12.95%
本文介绍了一种使用Python实现的高效算法,用于判断一个部分填充的数独是否符合数独的基本规则。通过利用集合和哈希表,该算法能够快速检查每一行、每一列以及每一个宫内的数字是否重复。
690

被折叠的 条评论
为什么被折叠?



