Skip to content

Commit 53605fd

Browse files
authored
feat: add solutions to lc problem: No.52 (#2471)
No.0052.N-Queens II
1 parent 65a6803 commit 53605fd

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

solution/0000-0099/0046.Permutations/README_EN.md

+22
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,28 @@ class Solution:
4848
return list(permutations(nums))
4949
```
5050

51+
```python
52+
class Solution:
53+
def permute(self, nums: List[int]) -> List[List[int]]:
54+
def dfs(i):
55+
if i == n:
56+
ans.append(t[:])
57+
return
58+
for j in range(n):
59+
if not vis[j]:
60+
vis[j] = True
61+
t[i] = nums[j]
62+
dfs(i + 1)
63+
vis[j] = False
64+
65+
n = len(nums)
66+
vis = [False] * n
67+
t = [0] * n
68+
ans = []
69+
dfs(0)
70+
return ans
71+
```
72+
5173
```java
5274
class Solution {
5375
private List<List<Integer>> ans = new ArrayList<>();

solution/0000-0099/0052.N-Queens II/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,36 @@ function totalNQueens(n: number): number {
198198
}
199199
```
200200

201+
```cs
202+
public class Solution {
203+
public int TotalNQueens(int n) {
204+
bool[] cols = new bool[10];
205+
bool[] dg = new bool[20];
206+
bool[] udg = new bool[20];
207+
int ans = 0;
208+
209+
void dfs(int i) {
210+
if (i == n) {
211+
ans++;
212+
return;
213+
}
214+
for (int j = 0; j < n; j++) {
215+
int a = i + j, b = i - j + n;
216+
if (cols[j] || dg[a] || udg[b]) {
217+
continue;
218+
}
219+
cols[j] = dg[a] = udg[b] = true;
220+
dfs(i + 1);
221+
cols[j] = dg[a] = udg[b] = false;
222+
}
223+
}
224+
225+
dfs(0);
226+
return ans;
227+
}
228+
}
229+
```
230+
201231
<!-- tabs:end -->
202232

203233
<!-- end -->

solution/0000-0099/0052.N-Queens II/README_EN.md

+30
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,36 @@ function totalNQueens(n: number): number {
190190
}
191191
```
192192

193+
```cs
194+
public class Solution {
195+
public int TotalNQueens(int n) {
196+
bool[] cols = new bool[10];
197+
bool[] dg = new bool[20];
198+
bool[] udg = new bool[20];
199+
int ans = 0;
200+
201+
void dfs(int i) {
202+
if (i == n) {
203+
ans++;
204+
return;
205+
}
206+
for (int j = 0; j < n; j++) {
207+
int a = i + j, b = i - j + n;
208+
if (cols[j] || dg[a] || udg[b]) {
209+
continue;
210+
}
211+
cols[j] = dg[a] = udg[b] = true;
212+
dfs(i + 1);
213+
cols[j] = dg[a] = udg[b] = false;
214+
}
215+
}
216+
217+
dfs(0);
218+
return ans;
219+
}
220+
}
221+
```
222+
193223
<!-- tabs:end -->
194224

195225
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class Solution {
2+
public int TotalNQueens(int n) {
3+
bool[] cols = new bool[10];
4+
bool[] dg = new bool[20];
5+
bool[] udg = new bool[20];
6+
int ans = 0;
7+
8+
void dfs(int i) {
9+
if (i == n) {
10+
ans++;
11+
return;
12+
}
13+
for (int j = 0; j < n; j++) {
14+
int a = i + j, b = i - j + n;
15+
if (cols[j] || dg[a] || udg[b]) {
16+
continue;
17+
}
18+
cols[j] = dg[a] = udg[b] = true;
19+
dfs(i + 1);
20+
cols[j] = dg[a] = udg[b] = false;
21+
}
22+
}
23+
24+
dfs(0);
25+
return ans;
26+
}
27+
}

0 commit comments

Comments
 (0)