A Python solution is here


  • 0
    M
    class Solution:
        def solveSudoku(self, board):
            """
            :type board: List[List[str]]
            :rtype: void Do not return anything, modify board in-place instead.
            """
            for i, grid in enumerate(board):
                for j, value in enumerate(grid):
                    if value == '.':
                        cands = self.isValid(board, i, j)
                        if len(cands) == 1:
                            board[i][j] = cands[0]
                            self.solveSudoku(board)
        
        def isValid(self, board, grid_pos, elem_pos):
            list = ['1','2','3','4','5','6','7','8','9']
            cols = []
            rows = []
            for i, grid in enumerate(board):
                if i%3 == grid_pos%3 or i//3 == grid_pos//3:
                    for j, elem in enumerate(grid): 
                        if i%3 == grid_pos%3 and j%3 == elem_pos%3 and elem != '.':
                            cols.append(elem)
                        if i//3 == grid_pos//3 and j//3 == elem_pos//3 and elem != '.':
                            rows.append(elem)
            cands = []
            for element in list:
                if element not in board[grid_pos] and element not in rows and element not in cols:
                    cands.append(element)
            return cands
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.