Skip to content

Commit 085460b

Browse files
committed
Merge branch 'master' of github.com:kedebug/leetcode
2 parents d33343a + 832edfa commit 085460b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

N-QueensII/N-QueensII.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int queens;
4+
int mask;
5+
int totalNQueens(int n) {
6+
// Start typing your C/C++ solution below
7+
// DO NOT write int main() function
8+
9+
mask = (1 << n) - 1;
10+
queens = 0;
11+
totalNQueensHelper(0, 0, 0);
12+
return queens;
13+
}
14+
15+
void totalNQueensHelper(int col, int ld, int rd) {
16+
if (col != mask) {
17+
int valid_pos = mask & (~(col | ld | rd));
18+
while (valid_pos) {
19+
int p = valid_pos & (-valid_pos);
20+
valid_pos -= p;
21+
totalNQueensHelper(col + p, (ld + p) << 1, (rd + p) >> 1);
22+
}
23+
}
24+
else
25+
queens += 1;
26+
}
27+
};

0 commit comments

Comments
 (0)