We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent f21637c commit 4445cdcCopy full SHA for 4445cdc
kotlin/0052-n-queens-ii.kt
@@ -0,0 +1,34 @@
1
+class Solution {
2
+ fun totalNQueens(n: Int): Int {
3
+
4
+ val cols = HashSet<Int>()
5
+ val pD = HashSet<Int>()
6
+ val nD = HashSet<Int>()
7
+ var res = 0
8
9
+ fun fill(row: Int) {
10
11
+ if(row == n) {
12
+ res++
13
+ return
14
+ }
15
16
+ for(col in 0 until n) {
17
+ if(cols.contains(col) || pD.contains(row + col) || nD.contains(row - col)) continue
18
19
+ cols.add(col)
20
+ pD.add(row + col)
21
+ nD.add(row - col)
22
23
+ fill(row+1)
24
25
+ cols.remove(col)
26
+ pD.remove(row + col)
27
+ nD.remove(row - col)
28
29
30
31
+ fill(0)
32
+ return res
33
34
+}
0 commit comments