Python solution , bit.


  • 0
    H
    class Solution(object):
    def solveNQueens(self, n):
        """
        :type n: int
        :rtype: List[List[str]] 
        """
        res = []
        upperlim = reduce(lambda x, i: x<<1|1, range(n+1))
        self.helper(res, [-1]*n, upperlim)
        return res
    
    def helper(self, res, queens, upperlim, ld=0, row=0, rd=0, idx=0):
        n = len(queens)
        if idx == n:
            res += ['.'*j +'Q'+ '.'*(n-j-1) for j in queens],
            return
        pos = upperlim & ~(ld | row | rd)
        while pos:
            p = pos & (~pos + 1)
            pos -= p
            queens[idx] = int(math.log(p, 2))
            self.helper(res, queens, upperlim, (ld+p)<<1, row+p, (rd+p)>>1, idx +1)

Log in to reply
 

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