Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.52
Browse files Browse the repository at this point in the history
No.0052.N-Queens II
  • Loading branch information
yanglbme committed Mar 20, 2024
1 parent 65a6803 commit dd535a2
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
22 changes: 22 additions & 0 deletions solution/0000-0099/0046.Permutations/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ class Solution:
return list(permutations(nums))
```

```python
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
def dfs(i):
if i == n:
ans.append(t[:])
return
for j in range(n):
if not vis[j]:
vis[j] = True
t[i] = nums[j]
dfs(i + 1)
vis[j] = False

n = len(nums)
vis = [False] * n
t = [0] * n
ans = []
dfs(0)
return ans
```

```java
class Solution {
private List<List<Integer>> ans = new ArrayList<>();
Expand Down
30 changes: 30 additions & 0 deletions solution/0000-0099/0052.N-Queens II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,36 @@ function totalNQueens(n: number): number {
}
```

```cs
public class Solution {
public int TotalNQueens(int n) {
bool[] cols = new bool[10];
bool[] dg = new bool[20];
bool[] udg = new bool[20];
int ans = 0;

void dfs(int i) {
if (i == n) {
ans++;
return;
}
for (int j = 0; j < n; j++) {
int a = i + j, b = i - j + n;
if (cols[j] || dg[a] || udg[b]) {
continue;
}
cols[j] = dg[a] = udg[b] = true;
dfs(i + 1);
cols[j] = dg[a] = udg[b] = false;
}
}

dfs(0);
return ans;
}
}
```

<!-- tabs:end -->

<!-- end -->
30 changes: 30 additions & 0 deletions solution/0000-0099/0052.N-Queens II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,36 @@ function totalNQueens(n: number): number {
}
```

```cs
public class Solution {
public int TotalNQueens(int n) {
bool[] cols = new bool[10];
bool[] dg = new bool[20];
bool[] udg = new bool[20];
int ans = 0;

void dfs(int i) {
if (i == n) {
ans++;
return;
}
for (int j = 0; j < n; j++) {
int a = i + j, b = i - j + n;
if (cols[j] || dg[a] || udg[b]) {
continue;
}
cols[j] = dg[a] = udg[b] = true;
dfs(i + 1);
cols[j] = dg[a] = udg[b] = false;
}
}

dfs(0);
return ans;
}
}
```

<!-- tabs:end -->

<!-- end -->
27 changes: 27 additions & 0 deletions solution/0000-0099/0052.N-Queens II/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class Solution {
public int TotalNQueens(int n) {
bool[] cols = new bool[10];
bool[] dg = new bool[20];
bool[] udg = new bool[20];
int ans = 0;

void dfs(int i) {
if (i == n) {
ans++;
return;
}
for (int j = 0; j < n; j++) {
int a = i + j, b = i - j + n;
if (cols[j] || dg[a] || udg[b]) {
continue;
}
cols[j] = dg[a] = udg[b] = true;
dfs(i + 1);
cols[j] = dg[a] = udg[b] = false;
}
}

dfs(0);
return ans;
}
}

0 comments on commit dd535a2

Please sign in to comment.