Skip to content

Commit 4445cdc

Browse files
authored
Create 0052-n-queens-ii.kt
1 parent f21637c commit 4445cdc

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

kotlin/0052-n-queens-ii.kt

+34
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)