本文主要是介绍【有效的数独】python,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
很好,超级暴力做法,能过就行,优雅个锤子啊
优雅的做法 ,绅士,噢听说叫模拟
很好,超级暴力做法,能过就行,优雅个锤子啊
我直接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 Falsedit[board[i][j]]=1dit=dict()for i in range(3,6):for j in range(3):if board[i][j] in dit and board[i][j]!='.':return Falsedit[board[i][j]]=1dit=dict()for i in range(6,9):for j in range(3):if board[i][j] in dit and board[i][j]!='.':return Falsedit[board[i][j]]=1dit=dict()for i in range(3):for j in range(3,6):if board[i][j] in dit and board[i][j]!='.':return Falsedit[board[i][j]]=1dit=dict()for i in range(3):for j in range(6,9):if board[i][j] in dit and board[i][j]!='.':return Falsedit[board[i][j]]=1dit=dict()for i in range(3,6):for j in range(3,6):if board[i][j] in dit and board[i][j]!='.':return Falsedit[board[i][j]]=1dit=dict()for i in range(3,6):for j in range(6,9):if board[i][j] in dit and board[i][j]!='.':return Falsedit[board[i][j]]=1dit=dict()for i in range(6,9):for j in range(3,6):if board[i][j] in dit and board[i][j]!='.':return Falsedit[board[i][j]]=1dit=dict()for i in range(6,9):for j in range(6,9):if board[i][j] in dit and board[i][j]!='.':return Falsedit[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 Falsedit[board[i][j]]=1dit=dict()#列判断for i in range(9):dit=dict()for j in range(9):if board[j][i] in dit and board[j][i]!='.':return Falsedit[board[j][i]]=1return 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 Falsesr.add(board[i][j])#列判断if board[j][i]!='.':if board[j][i] in sc:return Falsesc.add(board[j][i])#9宫格判断row,col=i//3*3+j//3,i%3*3+j%3if board[row][col]!='.':if board[row][col] in ss:return Falsess.add(board[row][col])return True
大佬tql,必须挂一波链接
这
这篇关于【有效的数独】python的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!