Share my short solution in Python

  • 0
        def solveSudoku(self, board):
            :type board: List[List[str]]
            :rtype: void Do not return anything, modify board in-place instead.
            def solver(board):
                for pi,row in enumerate(board):
                    for pj,c in enumerate(row):
                        if c == '.':
                            for x in set("123456789.")-set(board[pi])-set([board[k][pj] for k in range(9)])-set([board[pi/3*3+k1][pj/3*3+k2] for k1 in range(3) for k2 in range(3)]):
                                board[pi][pj] = x
                                if solver(board):
                                    return True
                            board[pi][pj] = '.'
                            return False
                return True

Log in to reply

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