12-lines 68ms python accepted DFS solution

  • 0
    def totalNQueens(self, n):
            result = [0]
            def DFS(cos, xps, xms):
                i = len(cos)
                if i == n: 
                    result[0] += 1
                for j in range(n):
                    if j not in cos and i+j not in xps and i-j not in xms:
                        DFS(cos+[j], xps+[i+j], xms+[i-j])
            DFS([], [], [])
            return result[0]

Log in to reply

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