leetcode: 52. N-Queens II

来源:互联网 发布:mac饥荒汉化mod 编辑:程序博客网 时间:2024/06/14 02:04

Q

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

这里写图片描述

AC

class Solution(object):    def totalNQueens(self, n):        """        :type n: int        :rtype: int        """        perms =[]        import itertools        for perm in itertools.permutations(range(n)):            diag = set()            tdiag = set()            conflict = False            for i, c in enumerate(perm):                d = i+c                td = i+n-c                if d in diag or td in tdiag:                    conflict = True                    break                diag.add(d)                tdiag.add(td)            if not conflict:                perms.append(perm)        return len(perms)


原创粉丝点击