This commit is contained in:
krahets
2023-11-27 02:32:06 +08:00
parent 32d5bd97aa
commit a4a23e2488
31 changed files with 179 additions and 213 deletions
@@ -66,7 +66,7 @@ comments: true
```csharp title="preorder_traversal_i_compact.cs"
/* 前序遍历:例题一 */
void PreOrder(TreeNode root) {
void PreOrder(TreeNode? root) {
if (root == null) {
return;
}
@@ -288,7 +288,7 @@ comments: true
```csharp title="preorder_traversal_ii_compact.cs"
/* 前序遍历:例题二 */
void PreOrder(TreeNode root) {
void PreOrder(TreeNode? root) {
if (root == null) {
return;
}
@@ -588,7 +588,7 @@ comments: true
```csharp title="preorder_traversal_iii_compact.cs"
/* 前序遍历:例题三 */
void PreOrder(TreeNode root) {
void PreOrder(TreeNode? root) {
// 剪枝
if (root == null || root.val == 3) {
return;
@@ -1267,7 +1267,7 @@ comments: true
// 尝试:做出选择,更新状态
MakeChoice(state, choice);
// 进行下一轮选择
Backtrack(state, new List<TreeNode> { choice.left, choice.right }, res);
Backtrack(state, [choice.left!, choice.right!], res);
// 回退:撤销选择,恢复到之前的状态
UndoChoice(state, choice);
}
@@ -207,7 +207,7 @@ comments: true
bool[] cols, bool[] diags1, bool[] diags2) {
// 当放置完所有行时,记录解
if (row == n) {
List<List<string>> copyState = new();
List<List<string>> copyState = [];
foreach (List<string> sRow in state) {
copyState.Add(new List<string>(sRow));
}
@@ -236,9 +236,9 @@ comments: true
/* 求解 N 皇后 */
List<List<List<string>>> NQueens(int n) {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
List<List<string>> state = new();
List<List<string>> state = [];
for (int i = 0; i < n; i++) {
List<string> row = new();
List<string> row = [];
for (int j = 0; j < n; j++) {
row.Add("#");
}
@@ -247,7 +247,7 @@ comments: true
bool[] cols = new bool[n]; // 记录列是否有皇后
bool[] diags1 = new bool[2 * n - 1]; // 记录主对角线是否有皇后
bool[] diags2 = new bool[2 * n - 1]; // 记录副对角线是否有皇后
List<List<List<string>>> res = new();
List<List<List<string>>> res = [];
Backtrack(0, n, state, res, cols, diags1, diags2);
@@ -187,8 +187,8 @@ comments: true
/* 全排列 I */
List<List<int>> PermutationsI(int[] nums) {
List<List<int>> res = new();
Backtrack(new List<int>(), nums, new bool[nums.Length], res);
List<List<int>> res = [];
Backtrack([], nums, new bool[nums.Length], res);
return res;
}
```
@@ -623,7 +623,7 @@ comments: true
return;
}
// 遍历所有选择
ISet<int> duplicated = new HashSet<int>();
HashSet<int> duplicated = [];
for (int i = 0; i < choices.Length; i++) {
int choice = choices[i];
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
@@ -643,8 +643,8 @@ comments: true
/* 全排列 II */
List<List<int>> PermutationsII(int[] nums) {
List<List<int>> res = new();
Backtrack(new List<int>(), nums, new bool[nums.Length], res);
List<List<int>> res = [];
Backtrack([], nums, new bool[nums.Length], res);
return res;
}
```
@@ -154,9 +154,9 @@ comments: true
/* 求解子集和 I(包含重复子集) */
List<List<int>> SubsetSumINaive(int[] nums, int target) {
List<int> state = new(); // 状态(子集)
List<int> state = []; // 状态(子集)
int total = 0; // 子集和
List<List<int>> res = new(); // 结果列表(子集列表)
List<List<int>> res = []; // 结果列表(子集列表)
Backtrack(state, target, total, nums, res);
return res;
}
@@ -609,10 +609,10 @@ comments: true
/* 求解子集和 I */
List<List<int>> SubsetSumI(int[] nums, int target) {
List<int> state = new(); // 状态(子集)
List<int> state = []; // 状态(子集)
Array.Sort(nums); // 对 nums 进行排序
int start = 0; // 遍历起始点
List<List<int>> res = new(); // 结果列表(子集列表)
List<List<int>> res = []; // 结果列表(子集列表)
Backtrack(state, target, nums, start, res);
return res;
}
@@ -1093,10 +1093,10 @@ comments: true
/* 求解子集和 II */
List<List<int>> SubsetSumII(int[] nums, int target) {
List<int> state = new(); // 状态(子集)
List<int> state = []; // 状态(子集)
Array.Sort(nums); // 对 nums 进行排序
int start = 0; // 遍历起始点
List<List<int>> res = new(); // 结果列表(子集列表)
List<List<int>> res = []; // 结果列表(子集列表)
Backtrack(state, target, nums, start, res);
return res;
}