From dd535a24cccf661e2cbd30beb22d5395380d6504 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 20 Mar 2024 21:01:27 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.52 No.0052.N-Queens II --- .../0000-0099/0046.Permutations/README_EN.md | 22 ++++++++++++++ solution/0000-0099/0052.N-Queens II/README.md | 30 +++++++++++++++++++ .../0000-0099/0052.N-Queens II/README_EN.md | 30 +++++++++++++++++++ .../0000-0099/0052.N-Queens II/Solution.cs | 27 +++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 solution/0000-0099/0052.N-Queens II/Solution.cs diff --git a/solution/0000-0099/0046.Permutations/README_EN.md b/solution/0000-0099/0046.Permutations/README_EN.md index 23175ef6a32af..a33157a3ea660 100644 --- a/solution/0000-0099/0046.Permutations/README_EN.md +++ b/solution/0000-0099/0046.Permutations/README_EN.md @@ -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> ans = new ArrayList<>(); diff --git a/solution/0000-0099/0052.N-Queens II/README.md b/solution/0000-0099/0052.N-Queens II/README.md index 8367d611b836c..c00e8a8da6b1d 100644 --- a/solution/0000-0099/0052.N-Queens II/README.md +++ b/solution/0000-0099/0052.N-Queens II/README.md @@ -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; + } +} +``` + diff --git a/solution/0000-0099/0052.N-Queens II/README_EN.md b/solution/0000-0099/0052.N-Queens II/README_EN.md index 4f2ba7d2248d7..25c7921efa695 100644 --- a/solution/0000-0099/0052.N-Queens II/README_EN.md +++ b/solution/0000-0099/0052.N-Queens II/README_EN.md @@ -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; + } +} +``` + diff --git a/solution/0000-0099/0052.N-Queens II/Solution.cs b/solution/0000-0099/0052.N-Queens II/Solution.cs new file mode 100644 index 0000000000000..8d503352ae9b4 --- /dev/null +++ b/solution/0000-0099/0052.N-Queens II/Solution.cs @@ -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; + } +} \ No newline at end of file