We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents d33343a + 832edfa commit 085460bCopy full SHA for 085460b
N-QueensII/N-QueensII.cpp
@@ -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