Python 11-line solution

  • 0
    class Solution(object):
        def solveNQueens(self, n):
            :type n: int
            :rtype: List[List[str]]
            res = []
            self.dfs([], res, n)
            return [['.'*j+'Q'+'.'*(n-1-j) for j in i]for i in res]
        def dfs(self, path, res, n):
            x = len(path)
            if x==n:
            for i in xrange(n):
                if (i not in path) and (x+i not in [a+b for a,b in enumerate(path)]) and (x-i not in [a-b for a,b in enumerate(path)]):
                    self.dfs(path + [i], res, n)

Log in to reply

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