36. 有效的数独
https://leetcode-cn.com/problems/valid-sudoku/
解法一:
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
for i in range(9):
rows = set() #每次i循环检测第i行
cols = set() #每次i循环检测第i列
cubes = set() #每次循环检测一块
for j in range(9):
char = board[i][j]
if char != '.':
if char not in rows:
rows.add(char)
else:
return False
char = board[j][i]
if char != '.':
if char not in cols:
cols.add(char)
else:
return False
rowIndex = 3*(i//3)
colIndex = 3*(i%3)
char = board[rowIndex + j//3][colIndex + j%3]
if char != '.':
if char not in cubes:
cubes.add(char)
else:
return False
return True解法二:
最后更新于