classSolution:defgetelements(self, board, i, j):
res =set()for k inrange(9):
res.add(board[k][j])for k inrange(9):
res.add(board[i][k])
indexa = i //3*3
indexb = j //3*3for ii inrange(indexa, indexa+3):for jj inrange(indexb, indexb+3):
res.add(board[ii][jj])return res
deftrackback(self, board, ia, jb):if ia ==8and jb ==8:returnTruefor i inrange(9):for j inrange(9):if board[i][j]!=".":continue
tmp = self.getelements(board, i, j)for k inrange(1,10):ifstr(k)in tmp:continueelse:
board[i][j]=str(k)if self.trackback(board, i, j):returnTrue
board[i][j]="."returnFalsereturnTruedefsolveSudoku(self, board: List[List[str]])->None:"""
Do not return anything, modify board in-place instead.
"""
self.trackback(board,0,0)
Python3 Solutionclass Solution: def getelements(self, board, i, j): res = set() for k in range(9): res.add(board[k][j]) for k in range(9): res.add(board[i][k]) indexa = i // 3 * 3 indexb