forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0052.py
30 lines (25 loc) · 864 Bytes
/
0052.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution:
def totalNQueens(self, n):
"""
:type n: int
:rtype: int
"""
cols, diag1, diag2 = set(), set(), set()
def getResult(n, diag1, diag2, cols, row):
if row == n:
return 1
res = 0
for col in range(n):
d1 = row + col
d2 = row - col
if d1 in diag1 or d2 in diag2 or col in cols:
continue
diag1.add(d1)
diag2.add(d2)
cols.add(col)
res += getResult(n, diag1, diag2, cols, row + 1)
diag1.remove(d1)
diag2.remove(d2)
cols.remove(col)
return res
return getResult(n, diag1, diag2, cols, 0)