8000
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.
1 parent ea06548 commit 832edfaCopy full SHA for 832edfa
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