Skip to content

feat: add solutions to lc problem: No.52 #2471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading