目录
很好,超级暴力做法,能过就行,优雅个锤子啊
优雅的做法 ,绅士,噢听说叫模拟
很好,超级暴力做法,能过就行,优雅个锤子啊
我直接11个2层for循环暴力秒杀
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
#9宫格判断
dit=dict()
for i in range(3):
for j in range(3):
if board[i][j] in dit and board[i][j]!='.':
return False
dit[board[i][j]]=1
dit=dict()
for i in range(3,6):
for j in range(3):
if board[i][j] in dit and board[i][j]!='.':
return False
dit[board[i][j]]=1
dit=dict()
for i in range(6,9):
for j in range(3):
if board[i][j] in dit and board[i][j]!='.':
return False
dit[board[i][j]]=1
dit=dict()
for i in range(3):
for j in range(3,6):
if board[i][j] in dit and board[i][j]!='.':
return False
dit[board[i][j]]=1
dit=dict()
for i in range(3):
for j in range(6,9):
if board[i][j] in dit and board[i][j]!='.':
return False
dit[board[i][j]]=1
dit=dict()
for i in range(3,6):
for j in range(3,6):
if board[i][j] in dit and board[i][j]!='.':
return False
dit[board[i][j]]=1
dit=dict()
for i in range(3,6):
for j in range(6,9):
if board[i][j] in dit and board[i][j]!='.':
return False
dit[board[i][j]]=1
dit=dict()
for i in range(6,9):
for j in range(3,6):
if board[i][j] in dit and board[i][j]!='.':
return False
dit[board[i][j]]=1
dit=dict()
for i in range(6,9):
for j in range(6,9):
if board[i][j] in dit and board[i][j]!='.':
return False
dit[board[i][j]]=1
#行判断
dit=dict()
for i in range(9):
dit=dict()
for j in range(9):
if board[i][j] in dit and board[i][j]!='.':
return False
dit[board[i][j]]=1
dit=dict()
#列判断
for i in range(9):
dit=dict()
for j in range(9):
if board[j][i] in dit and board[j][i]!='.':
return False
dit[board[j][i]]=1
return True
优雅的做法 ,绅士,噢听说叫模拟
其实判断过程(行列)都差不多,只是那个9宫格的计算判断真的厉害(固定i坐标,然后9次遍历j,将坐标映射到一个九宫格里面,就能完成一个九宫格的遍历)
row, col = i // 3 * 3 + j // 3, i % 3 * 3 + j % 3
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
for i in range(9):
#这里用的是set()集合,实际上用字典效果也一样
sr,sc,ss=set(),set(),set()
for j in range(9):
#行判断
if board[i][j]!='.':
if board[i][j] in sr:
return False
sr.add(board[i][j])
#列判断
if board[j][i]!='.':
if board[j][i] in sc:
return False
sc.add(board[j][i])
#9宫格判断
row,col=i//3*3+j//3,i%3*3+j%3
if board[row][col]!='.':
if board[row][col] in ss:
return False
ss.add(board[row][col])
return True
大佬tql,必须挂一波链接
这