feat(csharp) .NET 8.0 code migration (#966)

* .net 8.0 migration

* update docs

* revert change

* revert change and update appendix docs

* remove static

* Update binary_search_insertion.cs

* Update binary_search_insertion.cs

* Update binary_search_edge.cs

* Update binary_search_insertion.cs

* Update binary_search_edge.cs

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
hpstory
2023-11-26 23:18:44 +08:00
committed by GitHub
parent d960c99a1f
commit 56b20eff36
93 changed files with 539 additions and 487 deletions
+1 -1
View File
@@ -22,7 +22,7 @@ jobs:
strategy: strategy:
matrix: matrix:
os: [ubuntu-latest, macos-latest, windows-latest] os: [ubuntu-latest, macos-latest, windows-latest]
dotnet-version: ["6.0.x"] dotnet-version: ["8.0.x"]
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
+1 -1
View File
@@ -22,7 +22,7 @@ RUN for LANG in $LANGS; do \
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb && \ wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb && \
dpkg -i packages-microsoft-prod.deb && \ dpkg -i packages-microsoft-prod.deb && \
apt-get update && \ apt-get update && \
apt-get install -y dotnet-sdk-6.0 ;; \ apt-get install -y dotnet-sdk-8.0 ;; \
# More languages... # More languages...
*) \ *) \
echo "Warning: No installation workflow for $LANG" ;; \ echo "Warning: No installation workflow for $LANG" ;; \
+82
View File
@@ -4,3 +4,85 @@ csharp_new_line_before_open_brace = none
csharp_new_line_before_else = false csharp_new_line_before_else = false
csharp_new_line_before_catch = false csharp_new_line_before_catch = false
csharp_new_line_before_finally = false csharp_new_line_before_finally = false
csharp_indent_labels = one_less_than_current
csharp_using_directive_placement = outside_namespace:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_prefer_braces = true:silent
csharp_style_namespace_declarations = block_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_style_prefer_primary_constructors = true:suggestion
csharp_style_expression_bodied_methods = false:silent
csharp_style_expression_bodied_constructors = false:silent
csharp_style_expression_bodied_operators = false:silent
csharp_style_expression_bodied_properties = true:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_lambdas = true:silent
# CS8981: The type name only contains lower-cased ascii characters. Such names may become reserved for the language.
dotnet_diagnostic.CS8981.severity = silent
# IDE1006: Naming Styles
dotnet_diagnostic.IDE1006.severity = silent
# CA1822: Mark members as static
dotnet_diagnostic.CA1822.severity = silent
[*.{cs,vb}]
#### Naming styles ####
# Naming rules
dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i
dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case
dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case
# Symbol specifications
dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.interface.required_modifiers =
dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.types.required_modifiers =
dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers =
# Naming styles
dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.capitalization = pascal_case
dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case
dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
indent_size = 4
end_of_line = crlf
# IDE0040: Add accessibility modifiers
dotnet_diagnostic.IDE0040.severity = silent
# IDE0044: Add readonly modifier
dotnet_diagnostic.IDE0044.severity = silent
@@ -6,7 +6,7 @@ namespace hello_algo.chapter_array_and_linkedlist;
public class array { public class array {
/* 随机访问元素 */ /* 随机访问元素 */
public static int RandomAccess(int[] nums) { int RandomAccess(int[] nums) {
Random random = new(); Random random = new();
// 在区间 [0, nums.Length) 中随机抽取一个数字 // 在区间 [0, nums.Length) 中随机抽取一个数字
int randomIndex = random.Next(nums.Length); int randomIndex = random.Next(nums.Length);
@@ -16,7 +16,7 @@ public class array {
} }
/* 扩展数组长度 */ /* 扩展数组长度 */
public static int[] Extend(int[] nums, int enlarge) { int[] Extend(int[] nums, int enlarge) {
// 初始化一个扩展长度后的数组 // 初始化一个扩展长度后的数组
int[] res = new int[nums.Length + enlarge]; int[] res = new int[nums.Length + enlarge];
// 将原数组中的所有元素复制到新数组 // 将原数组中的所有元素复制到新数组
@@ -28,7 +28,7 @@ public class array {
} }
/* 在数组的索引 index 处插入元素 num */ /* 在数组的索引 index 处插入元素 num */
public static void Insert(int[] nums, int num, int index) { void Insert(int[] nums, int num, int index) {
// 把索引 index 以及之后的所有元素向后移动一位 // 把索引 index 以及之后的所有元素向后移动一位
for (int i = nums.Length - 1; i > index; i--) { for (int i = nums.Length - 1; i > index; i--) {
nums[i] = nums[i - 1]; nums[i] = nums[i - 1];
@@ -38,7 +38,7 @@ public class array {
} }
/* 删除索引 index 处元素 */ /* 删除索引 index 处元素 */
public static void Remove(int[] nums, int index) { void Remove(int[] nums, int index) {
// 把索引 index 之后的所有元素向前移动一位 // 把索引 index 之后的所有元素向前移动一位
for (int i = index; i < nums.Length - 1; i++) { for (int i = index; i < nums.Length - 1; i++) {
nums[i] = nums[i + 1]; nums[i] = nums[i + 1];
@@ -46,7 +46,7 @@ public class array {
} }
/* 遍历数组 */ /* 遍历数组 */
public static void Traverse(int[] nums) { void Traverse(int[] nums) {
int count = 0; int count = 0;
// 通过索引遍历数组 // 通过索引遍历数组
for (int i = 0; i < nums.Length; i++) { for (int i = 0; i < nums.Length; i++) {
@@ -59,7 +59,7 @@ public class array {
} }
/* 在数组中查找指定元素 */ /* 在数组中查找指定元素 */
public static int Find(int[] nums, int target) { int Find(int[] nums, int target) {
for (int i = 0; i < nums.Length; i++) { for (int i = 0; i < nums.Length; i++) {
if (nums[i] == target) if (nums[i] == target)
return i; return i;
@@ -68,17 +68,17 @@ public class array {
} }
/* 辅助函数,数组转字符串 */ /* 辅助函数,数组转字符串 */
public static string ToString(int[] nums) { string ToString(int[] nums) {
return string.Join(",", nums); return string.Join(",", nums);
} }
[Test] [Test]
public static void Test() { public void Test() {
// 初始化数组 // 初始化数组
int[] arr = new int[5]; int[] arr = new int[5];
Console.WriteLine("数组 arr = " + ToString(arr)); Console.WriteLine("数组 arr = " + ToString(arr));
int[] nums = { 1, 3, 2, 5, 4 }; int[] nums = [1, 3, 2, 5, 4];
Console.WriteLine("数组 nums = " + ToString(nums)); Console.WriteLine("数组 nums = " + ToString(nums));
// 随机访问 // 随机访问
@@ -6,14 +6,14 @@ namespace hello_algo.chapter_array_and_linkedlist;
public class linked_list { public class linked_list {
/* 在链表的节点 n0 之后插入节点 P */ /* 在链表的节点 n0 之后插入节点 P */
public static void Insert(ListNode n0, ListNode P) { void Insert(ListNode n0, ListNode P) {
ListNode? n1 = n0.next; ListNode? n1 = n0.next;
P.next = n1; P.next = n1;
n0.next = P; n0.next = P;
} }
/* 删除链表的节点 n0 之后的首个节点 */ /* 删除链表的节点 n0 之后的首个节点 */
public static void Remove(ListNode n0) { void Remove(ListNode n0) {
if (n0.next == null) if (n0.next == null)
return; return;
// n0 -> P -> n1 // n0 -> P -> n1
@@ -23,7 +23,7 @@ public class linked_list {
} }
/* 访问链表中索引为 index 的节点 */ /* 访问链表中索引为 index 的节点 */
public static ListNode? Access(ListNode head, int index) { ListNode? Access(ListNode? head, int index) {
for (int i = 0; i < index; i++) { for (int i = 0; i < index; i++) {
if (head == null) if (head == null)
return null; return null;
@@ -33,7 +33,7 @@ public class linked_list {
} }
/* 在链表中查找值为 target 的首个节点 */ /* 在链表中查找值为 target 的首个节点 */
public static int Find(ListNode head, int target) { int Find(ListNode? head, int target) {
int index = 0; int index = 0;
while (head != null) { while (head != null) {
if (head.val == target) if (head.val == target)
@@ -11,8 +11,8 @@ public class list {
public void Test() { public void Test() {
/* 初始化列表 */ /* 初始化列表 */
int[] numbers = new int[] { 1, 3, 2, 5, 4 }; int[] numbers = [1, 3, 2, 5, 4];
List<int> nums = numbers.ToList(); List<int> nums = [.. numbers];
Console.WriteLine("列表 nums = " + string.Join(",", nums)); Console.WriteLine("列表 nums = " + string.Join(",", nums));
/* 访问元素 */ /* 访问元素 */
@@ -55,7 +55,7 @@ public class list {
} }
/* 拼接两个列表 */ /* 拼接两个列表 */
List<int> nums1 = new() { 6, 8, 7, 10, 9 }; List<int> nums1 = [6, 8, 7, 10, 9];
nums.AddRange(nums1); nums.AddRange(nums1);
Console.WriteLine("将列表 nums1 拼接到 nums 之后,得到 nums = " + string.Join(",", nums)); Console.WriteLine("将列表 nums1 拼接到 nums 之后,得到 nums = " + string.Join(",", nums));
@@ -8,11 +8,11 @@ namespace hello_algo.chapter_backtracking;
public class n_queens { public class n_queens {
/* 回溯算法:N 皇后 */ /* 回溯算法:N 皇后 */
static void Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res, void Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,
bool[] cols, bool[] diags1, bool[] diags2) { bool[] cols, bool[] diags1, bool[] diags2) {
// 当放置完所有行时,记录解 // 当放置完所有行时,记录解
if (row == n) { if (row == n) {
List<List<string>> copyState = new(); List<List<string>> copyState = [];
foreach (List<string> sRow in state) { foreach (List<string> sRow in state) {
copyState.Add(new List<string>(sRow)); copyState.Add(new List<string>(sRow));
} }
@@ -39,11 +39,11 @@ public class n_queens {
} }
/* 求解 N 皇后 */ /* 求解 N 皇后 */
static List<List<List<string>>> NQueens(int n) { List<List<List<string>>> NQueens(int n) {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位 // 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
List<List<string>> state = new(); List<List<string>> state = [];
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
List<string> row = new(); List<string> row = [];
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
row.Add("#"); row.Add("#");
} }
@@ -52,7 +52,7 @@ public class n_queens {
bool[] cols = new bool[n]; // 记录列是否有皇后 bool[] cols = new bool[n]; // 记录列是否有皇后
bool[] diags1 = new bool[2 * n - 1]; // 记录主对角线是否有皇后 bool[] diags1 = new bool[2 * n - 1]; // 记录主对角线是否有皇后
bool[] diags2 = 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); Backtrack(0, n, state, res, cols, diags1, diags2);
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_backtracking;
public class permutations_i { public class permutations_i {
/* 回溯算法:全排列 I */ /* 回溯算法:全排列 I */
static void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) { void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
// 当状态长度等于元素数量时,记录解 // 当状态长度等于元素数量时,记录解
if (state.Count == choices.Length) { if (state.Count == choices.Length) {
res.Add(new List<int>(state)); res.Add(new List<int>(state));
@@ -32,15 +32,15 @@ public class permutations_i {
} }
/* 全排列 I */ /* 全排列 I */
static List<List<int>> PermutationsI(int[] nums) { List<List<int>> PermutationsI(int[] nums) {
List<List<int>> res = new(); List<List<int>> res = [];
Backtrack(new List<int>(), nums, new bool[nums.Length], res); Backtrack([], nums, new bool[nums.Length], res);
return res; return res;
} }
[Test] [Test]
public void Test() { public void Test() {
int[] nums = { 1, 2, 3 }; int[] nums = [1, 2, 3];
List<List<int>> res = PermutationsI(nums); List<List<int>> res = PermutationsI(nums);
@@ -8,14 +8,14 @@ namespace hello_algo.chapter_backtracking;
public class permutations_ii { public class permutations_ii {
/* 回溯算法:全排列 II */ /* 回溯算法:全排列 II */
static void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) { void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
// 当状态长度等于元素数量时,记录解 // 当状态长度等于元素数量时,记录解
if (state.Count == choices.Length) { if (state.Count == choices.Length) {
res.Add(new List<int>(state)); res.Add(new List<int>(state));
return; return;
} }
// 遍历所有选择 // 遍历所有选择
ISet<int> duplicated = new HashSet<int>(); HashSet<int> duplicated = [];
for (int i = 0; i < choices.Length; i++) { for (int i = 0; i < choices.Length; i++) {
int choice = choices[i]; int choice = choices[i];
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素 // 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
@@ -34,15 +34,15 @@ public class permutations_ii {
} }
/* 全排列 II */ /* 全排列 II */
static List<List<int>> PermutationsII(int[] nums) { List<List<int>> PermutationsII(int[] nums) {
List<List<int>> res = new(); List<List<int>> res = [];
Backtrack(new List<int>(), nums, new bool[nums.Length], res); Backtrack([], nums, new bool[nums.Length], res);
return res; return res;
} }
[Test] [Test]
public void Test() { public void Test() {
int[] nums = { 1, 2, 2 }; int[] nums = [1, 2, 2];
List<List<int>> res = PermutationsII(nums); List<List<int>> res = PermutationsII(nums);
@@ -7,10 +7,10 @@
namespace hello_algo.chapter_backtracking; namespace hello_algo.chapter_backtracking;
public class preorder_traversal_i_compact { public class preorder_traversal_i_compact {
static List<TreeNode> res; List<TreeNode> res = [];
/* 前序遍历:例题一 */ /* 前序遍历:例题一 */
static void PreOrder(TreeNode root) { void PreOrder(TreeNode? root) {
if (root == null) { if (root == null) {
return; return;
} }
@@ -24,12 +24,11 @@ public class preorder_traversal_i_compact {
[Test] [Test]
public void Test() { public void Test() {
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 }); TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
Console.WriteLine("\n初始化二叉树"); Console.WriteLine("\n初始化二叉树");
PrintUtil.PrintTree(root); PrintUtil.PrintTree(root);
// 前序遍历 // 前序遍历
res = new List<TreeNode>();
PreOrder(root); PreOrder(root);
Console.WriteLine("\n输出所有值为 7 的节点"); Console.WriteLine("\n输出所有值为 7 的节点");
@@ -7,11 +7,11 @@
namespace hello_algo.chapter_backtracking; namespace hello_algo.chapter_backtracking;
public class preorder_traversal_ii_compact { public class preorder_traversal_ii_compact {
static List<TreeNode> path; List<TreeNode> path = [];
static List<List<TreeNode>> res; List<List<TreeNode>> res = [];
/* 前序遍历:例题二 */ /* 前序遍历:例题二 */
static void PreOrder(TreeNode root) { void PreOrder(TreeNode? root) {
if (root == null) { if (root == null) {
return; return;
} }
@@ -29,13 +29,11 @@ public class preorder_traversal_ii_compact {
[Test] [Test]
public void Test() { public void Test() {
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 }); TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
Console.WriteLine("\n初始化二叉树"); Console.WriteLine("\n初始化二叉树");
PrintUtil.PrintTree(root); PrintUtil.PrintTree(root);
// 前序遍历 // 前序遍历
path = new List<TreeNode>();
res = new List<List<TreeNode>>();
PreOrder(root); PreOrder(root);
Console.WriteLine("\n输出所有根节点到节点 7 的路径"); Console.WriteLine("\n输出所有根节点到节点 7 的路径");
@@ -7,11 +7,11 @@
namespace hello_algo.chapter_backtracking; namespace hello_algo.chapter_backtracking;
public class preorder_traversal_iii_compact { public class preorder_traversal_iii_compact {
static List<TreeNode> path; List<TreeNode> path = [];
static List<List<TreeNode>> res; List<List<TreeNode>> res = [];
/* 前序遍历:例题三 */ /* 前序遍历:例题三 */
static void PreOrder(TreeNode root) { void PreOrder(TreeNode? root) {
// 剪枝 // 剪枝
if (root == null || root.val == 3) { if (root == null || root.val == 3) {
return; return;
@@ -30,13 +30,11 @@ public class preorder_traversal_iii_compact {
[Test] [Test]
public void Test() { public void Test() {
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 }); TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
Console.WriteLine("\n初始化二叉树"); Console.WriteLine("\n初始化二叉树");
PrintUtil.PrintTree(root); PrintUtil.PrintTree(root);
// 前序遍历 // 前序遍历
path = new List<TreeNode>();
res = new List<List<TreeNode>>();
PreOrder(root); PreOrder(root);
Console.WriteLine("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点"); Console.WriteLine("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点");
@@ -8,32 +8,32 @@ namespace hello_algo.chapter_backtracking;
public class preorder_traversal_iii_template { public class preorder_traversal_iii_template {
/* 判断当前状态是否为解 */ /* 判断当前状态是否为解 */
static bool IsSolution(List<TreeNode> state) { bool IsSolution(List<TreeNode> state) {
return state.Count != 0 && state[^1].val == 7; return state.Count != 0 && state[^1].val == 7;
} }
/* 记录解 */ /* 记录解 */
static void RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) { void RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
res.Add(new List<TreeNode>(state)); res.Add(new List<TreeNode>(state));
} }
/* 判断在当前状态下,该选择是否合法 */ /* 判断在当前状态下,该选择是否合法 */
static bool IsValid(List<TreeNode> state, TreeNode choice) { bool IsValid(List<TreeNode> state, TreeNode choice) {
return choice != null && choice.val != 3; return choice != null && choice.val != 3;
} }
/* 更新状态 */ /* 更新状态 */
static void MakeChoice(List<TreeNode> state, TreeNode choice) { void MakeChoice(List<TreeNode> state, TreeNode choice) {
state.Add(choice); state.Add(choice);
} }
/* 恢复状态 */ /* 恢复状态 */
static void UndoChoice(List<TreeNode> state, TreeNode choice) { void UndoChoice(List<TreeNode> state, TreeNode choice) {
state.RemoveAt(state.Count - 1); state.RemoveAt(state.Count - 1);
} }
/* 回溯算法:例题三 */ /* 回溯算法:例题三 */
static void Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) { void Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
// 检查是否为解 // 检查是否为解
if (IsSolution(state)) { if (IsSolution(state)) {
// 记录解 // 记录解
@@ -46,7 +46,7 @@ public class preorder_traversal_iii_template {
// 尝试:做出选择,更新状态 // 尝试:做出选择,更新状态
MakeChoice(state, choice); MakeChoice(state, choice);
// 进行下一轮选择 // 进行下一轮选择
Backtrack(state, new List<TreeNode> { choice.left, choice.right }, res); Backtrack(state, [choice.left!, choice.right!], res);
// 回退:撤销选择,恢复到之前的状态 // 回退:撤销选择,恢复到之前的状态
UndoChoice(state, choice); UndoChoice(state, choice);
} }
@@ -55,14 +55,14 @@ public class preorder_traversal_iii_template {
[Test] [Test]
public void Test() { public void Test() {
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 }); TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
Console.WriteLine("\n初始化二叉树"); Console.WriteLine("\n初始化二叉树");
PrintUtil.PrintTree(root); PrintUtil.PrintTree(root);
// 回溯算法 // 回溯算法
List<List<TreeNode>> res = new(); List<List<TreeNode>> res = [];
List<TreeNode> choices = new() { root }; List<TreeNode> choices = [root!];
Backtrack(new List<TreeNode>(), choices, res); Backtrack([], choices, res);
Console.WriteLine("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点"); Console.WriteLine("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点");
foreach (List<TreeNode> path in res) { foreach (List<TreeNode> path in res) {
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_backtracking;
public class subset_sum_i { public class subset_sum_i {
/* 回溯算法:子集和 I */ /* 回溯算法:子集和 I */
public static void Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) { void Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {
// 子集和等于 target 时,记录解 // 子集和等于 target 时,记录解
if (target == 0) { if (target == 0) {
res.Add(new List<int>(state)); res.Add(new List<int>(state));
@@ -32,18 +32,18 @@ public class subset_sum_i {
} }
/* 求解子集和 I */ /* 求解子集和 I */
public static List<List<int>> SubsetSumI(int[] nums, int target) { List<List<int>> SubsetSumI(int[] nums, int target) {
List<int> state = new(); // 状态(子集) List<int> state = []; // 状态(子集)
Array.Sort(nums); // 对 nums 进行排序 Array.Sort(nums); // 对 nums 进行排序
int start = 0; // 遍历起始点 int start = 0; // 遍历起始点
List<List<int>> res = new(); // 结果列表(子集列表) List<List<int>> res = []; // 结果列表(子集列表)
Backtrack(state, target, nums, start, res); Backtrack(state, target, nums, start, res);
return res; return res;
} }
[Test] [Test]
public void Test() { public void Test() {
int[] nums = { 3, 4, 5 }; int[] nums = [3, 4, 5];
int target = 9; int target = 9;
List<List<int>> res = SubsetSumI(nums, target); List<List<int>> res = SubsetSumI(nums, target);
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums) + ", target = " + target); Console.WriteLine("输入数组 nums = " + string.Join(", ", nums) + ", target = " + target);
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_backtracking;
public class subset_sum_i_naive { public class subset_sum_i_naive {
/* 回溯算法:子集和 I */ /* 回溯算法:子集和 I */
public static void Backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) { void Backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {
// 子集和等于 target 时,记录解 // 子集和等于 target 时,记录解
if (total == target) { if (total == target) {
res.Add(new List<int>(state)); res.Add(new List<int>(state));
@@ -30,17 +30,17 @@ public class subset_sum_i_naive {
} }
/* 求解子集和 I(包含重复子集) */ /* 求解子集和 I(包含重复子集) */
public static List<List<int>> SubsetSumINaive(int[] nums, int target) { List<List<int>> SubsetSumINaive(int[] nums, int target) {
List<int> state = new(); // 状态(子集) List<int> state = []; // 状态(子集)
int total = 0; // 子集和 int total = 0; // 子集和
List<List<int>> res = new(); // 结果列表(子集列表) List<List<int>> res = []; // 结果列表(子集列表)
Backtrack(state, target, total, nums, res); Backtrack(state, target, total, nums, res);
return res; return res;
} }
[Test] [Test]
public void Test() { public void Test() {
int[] nums = { 3, 4, 5 }; int[] nums = [3, 4, 5];
int target = 9; int target = 9;
List<List<int>> res = SubsetSumINaive(nums, target); List<List<int>> res = SubsetSumINaive(nums, target);
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums) + ", target = " + target); Console.WriteLine("输入数组 nums = " + string.Join(", ", nums) + ", target = " + target);
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_backtracking;
public class subset_sum_ii { public class subset_sum_ii {
/* 回溯算法:子集和 II */ /* 回溯算法:子集和 II */
public static void Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) { void Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {
// 子集和等于 target 时,记录解 // 子集和等于 target 时,记录解
if (target == 0) { if (target == 0) {
res.Add(new List<int>(state)); res.Add(new List<int>(state));
@@ -37,18 +37,18 @@ public class subset_sum_ii {
} }
/* 求解子集和 II */ /* 求解子集和 II */
public static List<List<int>> SubsetSumII(int[] nums, int target) { List<List<int>> SubsetSumII(int[] nums, int target) {
List<int> state = new(); // 状态(子集) List<int> state = []; // 状态(子集)
Array.Sort(nums); // 对 nums 进行排序 Array.Sort(nums); // 对 nums 进行排序
int start = 0; // 遍历起始点 int start = 0; // 遍历起始点
List<List<int>> res = new(); // 结果列表(子集列表) List<List<int>> res = []; // 结果列表(子集列表)
Backtrack(state, target, nums, start, res); Backtrack(state, target, nums, start, res);
return res; return res;
} }
[Test] [Test]
public void Test() { public void Test() {
int[] nums = { 4, 4, 5 }; int[] nums = [4, 4, 5];
int target = 9; int target = 9;
List<List<int>> res = SubsetSumII(nums, target); List<List<int>> res = SubsetSumII(nums, target);
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums) + ", target = " + target); Console.WriteLine("输入数组 nums = " + string.Join(", ", nums) + ", target = " + target);
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_computational_complexity;
public class iteration { public class iteration {
/* for 循环 */ /* for 循环 */
public static int ForLoop(int n) { int ForLoop(int n) {
int res = 0; int res = 0;
// 循环求和 1, 2, ..., n-1, n // 循环求和 1, 2, ..., n-1, n
for (int i = 1; i <= n; i++) { for (int i = 1; i <= n; i++) {
@@ -18,7 +18,7 @@ public class iteration {
} }
/* while 循环 */ /* while 循环 */
public static int WhileLoop(int n) { int WhileLoop(int n) {
int res = 0; int res = 0;
int i = 1; // 初始化条件变量 int i = 1; // 初始化条件变量
// 循环求和 1, 2, ..., n-1, n // 循环求和 1, 2, ..., n-1, n
@@ -30,7 +30,7 @@ public class iteration {
} }
/* while 循环(两次更新) */ /* while 循环(两次更新) */
public static int WhileLoopII(int n) { int WhileLoopII(int n) {
int res = 0; int res = 0;
int i = 1; // 初始化条件变量 int i = 1; // 初始化条件变量
// 循环求和 1, 2, 4, 5... // 循环求和 1, 2, 4, 5...
@@ -44,7 +44,7 @@ public class iteration {
} }
/* 双层 for 循环 */ /* 双层 for 循环 */
public static string NestedForLoop(int n) { string NestedForLoop(int n) {
StringBuilder res = new(); StringBuilder res = new();
// 循环 i = 1, 2, ..., n-1, n // 循环 i = 1, 2, ..., n-1, n
for (int i = 1; i <= n; i++) { for (int i = 1; i <= n; i++) {
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_computational_complexity;
public class recursion { public class recursion {
/* 递归 */ /* 递归 */
public int Recur(int n) { int Recur(int n) {
// 终止条件 // 终止条件
if (n == 1) if (n == 1)
return 1; return 1;
@@ -19,7 +19,7 @@ public class recursion {
} }
/* 使用迭代模拟递归 */ /* 使用迭代模拟递归 */
public static int ForLoopRecur(int n) { int ForLoopRecur(int n) {
// 使用一个显式的栈来模拟系统调用栈 // 使用一个显式的栈来模拟系统调用栈
Stack<int> stack = new(); Stack<int> stack = new();
int res = 0; int res = 0;
@@ -38,7 +38,7 @@ public class recursion {
} }
/* 尾递归 */ /* 尾递归 */
public int TailRecur(int n, int res) { int TailRecur(int n, int res) {
// 终止条件 // 终止条件
if (n == 0) if (n == 0)
return res; return res;
@@ -47,7 +47,7 @@ public class recursion {
} }
/* 斐波那契数列:递归 */ /* 斐波那契数列:递归 */
public int Fib(int n) { int Fib(int n) {
// 终止条件 f(1) = 0, f(2) = 1 // 终止条件 f(1) = 0, f(2) = 1
if (n == 1 || n == 2) if (n == 1 || n == 2)
return n - 1; return n - 1;
@@ -8,13 +8,13 @@ namespace hello_algo.chapter_computational_complexity;
public class space_complexity { public class space_complexity {
/* 函数 */ /* 函数 */
static int Function() { int Function() {
// 执行某些操作 // 执行某些操作
return 0; return 0;
} }
/* 常数阶 */ /* 常数阶 */
static void Constant(int n) { void Constant(int n) {
// 常量、变量、对象占用 O(1) 空间 // 常量、变量、对象占用 O(1) 空间
int a = 0; int a = 0;
int b = 0; int b = 0;
@@ -31,36 +31,36 @@ public class space_complexity {
} }
/* 线性阶 */ /* 线性阶 */
static void Linear(int n) { void Linear(int n) {
// 长度为 n 的数组占用 O(n) 空间 // 长度为 n 的数组占用 O(n) 空间
int[] nums = new int[n]; int[] nums = new int[n];
// 长度为 n 的列表占用 O(n) 空间 // 长度为 n 的列表占用 O(n) 空间
List<ListNode> nodes = new(); List<ListNode> nodes = [];
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
nodes.Add(new ListNode(i)); nodes.Add(new ListNode(i));
} }
// 长度为 n 的哈希表占用 O(n) 空间 // 长度为 n 的哈希表占用 O(n) 空间
Dictionary<int, string> map = new(); Dictionary<int, string> map = [];
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
map.Add(i, i.ToString()); map.Add(i, i.ToString());
} }
} }
/* 线性阶(递归实现) */ /* 线性阶(递归实现) */
static void LinearRecur(int n) { void LinearRecur(int n) {
Console.WriteLine("递归 n = " + n); Console.WriteLine("递归 n = " + n);
if (n == 1) return; if (n == 1) return;
LinearRecur(n - 1); LinearRecur(n - 1);
} }
/* 平方阶 */ /* 平方阶 */
static void Quadratic(int n) { void Quadratic(int n) {
// 矩阵占用 O(n^2) 空间 // 矩阵占用 O(n^2) 空间
int[,] numMatrix = new int[n, n]; int[,] numMatrix = new int[n, n];
// 二维列表占用 O(n^2) 空间 // 二维列表占用 O(n^2) 空间
List<List<int>> numList = new(); List<List<int>> numList = [];
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
List<int> tmp = new(); List<int> tmp = [];
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
tmp.Add(0); tmp.Add(0);
} }
@@ -69,7 +69,7 @@ public class space_complexity {
} }
/* 平方阶(递归实现) */ /* 平方阶(递归实现) */
static int QuadraticRecur(int n) { int QuadraticRecur(int n) {
if (n <= 0) return 0; if (n <= 0) return 0;
int[] nums = new int[n]; int[] nums = new int[n];
Console.WriteLine("递归 n = " + n + " 中的 nums 长度 = " + nums.Length); Console.WriteLine("递归 n = " + n + " 中的 nums 长度 = " + nums.Length);
@@ -77,7 +77,7 @@ public class space_complexity {
} }
/* 指数阶(建立满二叉树) */ /* 指数阶(建立满二叉树) */
static TreeNode? BuildTree(int n) { TreeNode? BuildTree(int n) {
if (n == 0) return null; if (n == 0) return null;
TreeNode root = new(0) { TreeNode root = new(0) {
left = BuildTree(n - 1), left = BuildTree(n - 1),
@@ -9,7 +9,7 @@ namespace hello_algo.chapter_computational_complexity;
public class time_complexity { public class time_complexity {
void Algorithm(int n) { void Algorithm(int n) {
int a = 1; // +0(技巧 1 int a = 1; // +0(技巧 1
a = a + n; // +0(技巧 1 a += n; // +0(技巧 1
// +n(技巧 2 // +n(技巧 2
for (int i = 0; i < 5 * n + 1; i++) { for (int i = 0; i < 5 * n + 1; i++) {
Console.WriteLine(0); Console.WriteLine(0);
@@ -26,12 +26,14 @@ public class time_complexity {
void AlgorithmA(int n) { void AlgorithmA(int n) {
Console.WriteLine(0); Console.WriteLine(0);
} }
// 算法 B 时间复杂度:线性阶 // 算法 B 时间复杂度:线性阶
void AlgorithmB(int n) { void AlgorithmB(int n) {
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
Console.WriteLine(0); Console.WriteLine(0);
} }
} }
// 算法 C 时间复杂度:常数阶 // 算法 C 时间复杂度:常数阶
void AlgorithmC(int n) { void AlgorithmC(int n) {
for (int i = 0; i < 1000000; i++) { for (int i = 0; i < 1000000; i++) {
@@ -40,7 +42,7 @@ public class time_complexity {
} }
/* 常数阶 */ /* 常数阶 */
static int Constant(int n) { int Constant(int n) {
int count = 0; int count = 0;
int size = 100000; int size = 100000;
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
@@ -49,7 +51,7 @@ public class time_complexity {
} }
/* 线性阶 */ /* 线性阶 */
static int Linear(int n) { int Linear(int n) {
int count = 0; int count = 0;
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
count++; count++;
@@ -57,7 +59,7 @@ public class time_complexity {
} }
/* 线性阶(遍历数组) */ /* 线性阶(遍历数组) */
static int ArrayTraversal(int[] nums) { int ArrayTraversal(int[] nums) {
int count = 0; int count = 0;
// 循环次数与数组长度成正比 // 循环次数与数组长度成正比
foreach (int num in nums) { foreach (int num in nums) {
@@ -67,7 +69,7 @@ public class time_complexity {
} }
/* 平方阶 */ /* 平方阶 */
static int Quadratic(int n) { int Quadratic(int n) {
int count = 0; int count = 0;
// 循环次数与数组长度成平方关系 // 循环次数与数组长度成平方关系
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
@@ -79,7 +81,7 @@ public class time_complexity {
} }
/* 平方阶(冒泡排序) */ /* 平方阶(冒泡排序) */
static int BubbleSort(int[] nums) { int BubbleSort(int[] nums) {
int count = 0; // 计数器 int count = 0; // 计数器
// 外循环:未排序区间为 [0, i] // 外循环:未排序区间为 [0, i]
for (int i = nums.Length - 1; i > 0; i--) { for (int i = nums.Length - 1; i > 0; i--) {
@@ -96,7 +98,7 @@ public class time_complexity {
} }
/* 指数阶(循环实现) */ /* 指数阶(循环实现) */
static int Exponential(int n) { int Exponential(int n) {
int count = 0, bas = 1; int count = 0, bas = 1;
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) // 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
@@ -110,29 +112,29 @@ public class time_complexity {
} }
/* 指数阶(递归实现) */ /* 指数阶(递归实现) */
static int ExpRecur(int n) { int ExpRecur(int n) {
if (n == 1) return 1; if (n == 1) return 1;
return ExpRecur(n - 1) + ExpRecur(n - 1) + 1; return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;
} }
/* 对数阶(循环实现) */ /* 对数阶(循环实现) */
static int Logarithmic(float n) { int Logarithmic(float n) {
int count = 0; int count = 0;
while (n > 1) { while (n > 1) {
n = n / 2; n /= 2;
count++; count++;
} }
return count; return count;
} }
/* 对数阶(递归实现) */ /* 对数阶(递归实现) */
static int LogRecur(float n) { int LogRecur(float n) {
if (n <= 1) return 0; if (n <= 1) return 0;
return LogRecur(n / 2) + 1; return LogRecur(n / 2) + 1;
} }
/* 线性对数阶 */ /* 线性对数阶 */
static int LinearLogRecur(float n) { int LinearLogRecur(float n) {
if (n <= 1) return 1; if (n <= 1) return 1;
int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2); int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
@@ -142,7 +144,7 @@ public class time_complexity {
} }
/* 阶乘阶(递归实现) */ /* 阶乘阶(递归实现) */
static int FactorialRecur(int n) { int FactorialRecur(int n) {
if (n == 0) return 1; if (n == 0) return 1;
int count = 0; int count = 0;
// 从 1 个分裂出 n 个 // 从 1 个分裂出 n 个
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_computational_complexity;
public class worst_best_time_complexity { public class worst_best_time_complexity {
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ /* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
static int[] RandomNumbers(int n) { int[] RandomNumbers(int n) {
int[] nums = new int[n]; int[] nums = new int[n];
// 生成数组 nums = { 1, 2, 3, ..., n } // 生成数组 nums = { 1, 2, 3, ..., n }
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
@@ -24,7 +24,7 @@ public class worst_best_time_complexity {
} }
/* 查找数组 nums 中数字 1 所在索引 */ /* 查找数组 nums 中数字 1 所在索引 */
static int FindOne(int[] nums) { int FindOne(int[] nums) {
for (int i = 0; i < nums.Length; i++) { for (int i = 0; i < nums.Length; i++) {
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_divide_and_conquer;
public class binary_search_recur { public class binary_search_recur {
/* 二分查找:问题 f(i, j) */ /* 二分查找:问题 f(i, j) */
public int DFS(int[] nums, int target, int i, int j) { int DFS(int[] nums, int target, int i, int j) {
// 若区间为空,代表无目标元素,则返回 -1 // 若区间为空,代表无目标元素,则返回 -1
if (i > j) { if (i > j) {
return -1; return -1;
@@ -28,7 +28,7 @@ public class binary_search_recur {
} }
/* 二分查找 */ /* 二分查找 */
public int BinarySearch(int[] nums, int target) { int BinarySearch(int[] nums, int target) {
int n = nums.Length; int n = nums.Length;
// 求解问题 f(0, n-1) // 求解问题 f(0, n-1)
return DFS(nums, target, 0, n - 1); return DFS(nums, target, 0, n - 1);
@@ -37,7 +37,7 @@ public class binary_search_recur {
[Test] [Test]
public void Test() { public void Test() {
int target = 6; int target = 6;
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 }; int[] nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
// 二分查找(双闭区间) // 二分查找(双闭区间)
int index = BinarySearch(nums, target); int index = BinarySearch(nums, target);
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_divide_and_conquer;
public class build_tree { public class build_tree {
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
public TreeNode DFS(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) { TreeNode? DFS(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {
// 子树区间为空时终止 // 子树区间为空时终止
if (r - l < 0) if (r - l < 0)
return null; return null;
@@ -25,24 +25,24 @@ public class build_tree {
} }
/* 构建二叉树 */ /* 构建二叉树 */
public TreeNode BuildTree(int[] preorder, int[] inorder) { TreeNode? BuildTree(int[] preorder, int[] inorder) {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
Dictionary<int, int> inorderMap = new(); Dictionary<int, int> inorderMap = [];
for (int i = 0; i < inorder.Length; i++) { for (int i = 0; i < inorder.Length; i++) {
inorderMap.TryAdd(inorder[i], i); inorderMap.TryAdd(inorder[i], i);
} }
TreeNode root = DFS(preorder, inorderMap, 0, 0, inorder.Length - 1); TreeNode? root = DFS(preorder, inorderMap, 0, 0, inorder.Length - 1);
return root; return root;
} }
[Test] [Test]
public void Test() { public void Test() {
int[] preorder = { 3, 9, 2, 1, 7 }; int[] preorder = [3, 9, 2, 1, 7];
int[] inorder = { 9, 3, 1, 2, 7 }; int[] inorder = [9, 3, 1, 2, 7];
Console.WriteLine("前序遍历 = " + string.Join(", ", preorder)); Console.WriteLine("前序遍历 = " + string.Join(", ", preorder));
Console.WriteLine("中序遍历 = " + string.Join(", ", inorder)); Console.WriteLine("中序遍历 = " + string.Join(", ", inorder));
TreeNode root = BuildTree(preorder, inorder); TreeNode? root = BuildTree(preorder, inorder);
Console.WriteLine("构建的二叉树为:"); Console.WriteLine("构建的二叉树为:");
PrintUtil.PrintTree(root); PrintUtil.PrintTree(root);
} }
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_divide_and_conquer;
public class hanota { public class hanota {
/* 移动一个圆盘 */ /* 移动一个圆盘 */
public void Move(List<int> src, List<int> tar) { void Move(List<int> src, List<int> tar) {
// 从 src 顶部拿出一个圆盘 // 从 src 顶部拿出一个圆盘
int pan = src[^1]; int pan = src[^1];
src.RemoveAt(src.Count - 1); src.RemoveAt(src.Count - 1);
@@ -17,7 +17,7 @@ public class hanota {
} }
/* 求解汉诺塔:问题 f(i) */ /* 求解汉诺塔:问题 f(i) */
public void DFS(int i, List<int> src, List<int> buf, List<int> tar) { void DFS(int i, List<int> src, List<int> buf, List<int> tar) {
// 若 src 只剩下一个圆盘,则直接将其移到 tar // 若 src 只剩下一个圆盘,则直接将其移到 tar
if (i == 1) { if (i == 1) {
Move(src, tar); Move(src, tar);
@@ -32,7 +32,7 @@ public class hanota {
} }
/* 求解汉诺塔 */ /* 求解汉诺塔 */
public void SolveHanota(List<int> A, List<int> B, List<int> C) { void SolveHanota(List<int> A, List<int> B, List<int> C) {
int n = A.Count; int n = A.Count;
// 将 A 顶部 n 个圆盘借助 B 移到 C // 将 A 顶部 n 个圆盘借助 B 移到 C
DFS(n, A, B, C); DFS(n, A, B, C);
@@ -41,9 +41,9 @@ public class hanota {
[Test] [Test]
public void Test() { public void Test() {
// 列表尾部是柱子顶部 // 列表尾部是柱子顶部
List<int> A = new() { 5, 4, 3, 2, 1 }; List<int> A = [5, 4, 3, 2, 1];
List<int> B = new(); List<int> B = [];
List<int> C = new(); List<int> C = [];
Console.WriteLine("初始状态下:"); Console.WriteLine("初始状态下:");
Console.WriteLine("A = " + string.Join(", ", A)); Console.WriteLine("A = " + string.Join(", ", A));
Console.WriteLine("B = " + string.Join(", ", B)); Console.WriteLine("B = " + string.Join(", ", B));
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class climbing_stairs_backtrack { public class climbing_stairs_backtrack {
/* 回溯 */ /* 回溯 */
public void Backtrack(List<int> choices, int state, int n, List<int> res) { void Backtrack(List<int> choices, int state, int n, List<int> res) {
// 当爬到第 n 阶时,方案数量加 1 // 当爬到第 n 阶时,方案数量加 1
if (state == n) if (state == n)
res[0]++; res[0]++;
@@ -24,10 +24,10 @@ public class climbing_stairs_backtrack {
} }
/* 爬楼梯:回溯 */ /* 爬楼梯:回溯 */
public int ClimbingStairsBacktrack(int n) { int ClimbingStairsBacktrack(int n) {
List<int> choices = new() { 1, 2 }; // 可选择向上爬 1 或 2 阶 List<int> choices = [1, 2]; // 可选择向上爬 1 或 2 阶
int state = 0; // 从第 0 阶开始爬 int state = 0; // 从第 0 阶开始爬
List<int> res = new() { 0 }; // 使用 res[0] 记录方案数量 List<int> res = [0]; // 使用 res[0] 记录方案数量
Backtrack(choices, state, n, res); Backtrack(choices, state, n, res);
return res[0]; return res[0];
} }
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class climbing_stairs_constraint_dp { public class climbing_stairs_constraint_dp {
/* 带约束爬楼梯:动态规划 */ /* 带约束爬楼梯:动态规划 */
public int ClimbingStairsConstraintDP(int n) { int ClimbingStairsConstraintDP(int n) {
if (n == 1 || n == 2) { if (n == 1 || n == 2) {
return 1; return 1;
} }
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class climbing_stairs_dfs { public class climbing_stairs_dfs {
/* 搜索 */ /* 搜索 */
public int DFS(int i) { int DFS(int i) {
// 已知 dp[1] 和 dp[2] ,返回之 // 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 || i == 2) if (i == 1 || i == 2)
return i; return i;
@@ -18,7 +18,7 @@ public class climbing_stairs_dfs {
} }
/* 爬楼梯:搜索 */ /* 爬楼梯:搜索 */
public int ClimbingStairsDFS(int n) { int ClimbingStairsDFS(int n) {
return DFS(n); return DFS(n);
} }
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class climbing_stairs_dfs_mem { public class climbing_stairs_dfs_mem {
/* 记忆化搜索 */ /* 记忆化搜索 */
public int DFS(int i, int[] mem) { int DFS(int i, int[] mem) {
// 已知 dp[1] 和 dp[2] ,返回之 // 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 || i == 2) if (i == 1 || i == 2)
return i; return i;
@@ -23,7 +23,7 @@ public class climbing_stairs_dfs_mem {
} }
/* 爬楼梯:记忆化搜索 */ /* 爬楼梯:记忆化搜索 */
public int ClimbingStairsDFSMem(int n) { int ClimbingStairsDFSMem(int n) {
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录 // mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
int[] mem = new int[n + 1]; int[] mem = new int[n + 1];
Array.Fill(mem, -1); Array.Fill(mem, -1);
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class climbing_stairs_dp { public class climbing_stairs_dp {
/* 爬楼梯:动态规划 */ /* 爬楼梯:动态规划 */
public int ClimbingStairsDP(int n) { int ClimbingStairsDP(int n) {
if (n == 1 || n == 2) if (n == 1 || n == 2)
return n; return n;
// 初始化 dp 表,用于存储子问题的解 // 初始化 dp 表,用于存储子问题的解
@@ -24,7 +24,7 @@ public class climbing_stairs_dp {
} }
/* 爬楼梯:空间优化后的动态规划 */ /* 爬楼梯:空间优化后的动态规划 */
public int ClimbingStairsDPComp(int n) { int ClimbingStairsDPComp(int n) {
if (n == 1 || n == 2) if (n == 1 || n == 2)
return n; return n;
int a = 1, b = 2; int a = 1, b = 2;
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class coin_change { public class coin_change {
/* 零钱兑换:动态规划 */ /* 零钱兑换:动态规划 */
public int CoinChangeDP(int[] coins, int amt) { int CoinChangeDP(int[] coins, int amt) {
int n = coins.Length; int n = coins.Length;
int MAX = amt + 1; int MAX = amt + 1;
// 初始化 dp 表 // 初始化 dp 表
@@ -33,7 +33,7 @@ public class coin_change {
} }
/* 零钱兑换:空间优化后的动态规划 */ /* 零钱兑换:空间优化后的动态规划 */
public int CoinChangeDPComp(int[] coins, int amt) { int CoinChangeDPComp(int[] coins, int amt) {
int n = coins.Length; int n = coins.Length;
int MAX = amt + 1; int MAX = amt + 1;
// 初始化 dp 表 // 初始化 dp 表
@@ -57,7 +57,7 @@ public class coin_change {
[Test] [Test]
public void Test() { public void Test() {
int[] coins = { 1, 2, 5 }; int[] coins = [1, 2, 5];
int amt = 4; int amt = 4;
// 动态规划 // 动态规划
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class coin_change_ii { public class coin_change_ii {
/* 零钱兑换 II:动态规划 */ /* 零钱兑换 II:动态规划 */
public int CoinChangeIIDP(int[] coins, int amt) { int CoinChangeIIDP(int[] coins, int amt) {
int n = coins.Length; int n = coins.Length;
// 初始化 dp 表 // 初始化 dp 表
int[,] dp = new int[n + 1, amt + 1]; int[,] dp = new int[n + 1, amt + 1];
@@ -32,7 +32,7 @@ public class coin_change_ii {
} }
/* 零钱兑换 II:空间优化后的动态规划 */ /* 零钱兑换 II:空间优化后的动态规划 */
public int CoinChangeIIDPComp(int[] coins, int amt) { int CoinChangeIIDPComp(int[] coins, int amt) {
int n = coins.Length; int n = coins.Length;
// 初始化 dp 表 // 初始化 dp 表
int[] dp = new int[amt + 1]; int[] dp = new int[amt + 1];
@@ -54,7 +54,7 @@ public class coin_change_ii {
[Test] [Test]
public void Test() { public void Test() {
int[] coins = { 1, 2, 5 }; int[] coins = [1, 2, 5];
int amt = 5; int amt = 5;
// 动态规划 // 动态规划
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class edit_distance { public class edit_distance {
/* 编辑距离:暴力搜索 */ /* 编辑距离:暴力搜索 */
public int EditDistanceDFS(string s, string t, int i, int j) { int EditDistanceDFS(string s, string t, int i, int j) {
// 若 s 和 t 都为空,则返回 0 // 若 s 和 t 都为空,则返回 0
if (i == 0 && j == 0) if (i == 0 && j == 0)
return 0; return 0;
@@ -30,7 +30,7 @@ public class edit_distance {
} }
/* 编辑距离:记忆化搜索 */ /* 编辑距离:记忆化搜索 */
public int EditDistanceDFSMem(string s, string t, int[][] mem, int i, int j) { int EditDistanceDFSMem(string s, string t, int[][] mem, int i, int j) {
// 若 s 和 t 都为空,则返回 0 // 若 s 和 t 都为空,则返回 0
if (i == 0 && j == 0) if (i == 0 && j == 0)
return 0; return 0;
@@ -56,7 +56,7 @@ public class edit_distance {
} }
/* 编辑距离:动态规划 */ /* 编辑距离:动态规划 */
public int EditDistanceDP(string s, string t) { int EditDistanceDP(string s, string t) {
int n = s.Length, m = t.Length; int n = s.Length, m = t.Length;
int[,] dp = new int[n + 1, m + 1]; int[,] dp = new int[n + 1, m + 1];
// 状态转移:首行首列 // 状态转移:首行首列
@@ -82,7 +82,7 @@ public class edit_distance {
} }
/* 编辑距离:空间优化后的动态规划 */ /* 编辑距离:空间优化后的动态规划 */
public int EditDistanceDPComp(string s, string t) { int EditDistanceDPComp(string s, string t) {
int n = s.Length, m = t.Length; int n = s.Length, m = t.Length;
int[] dp = new int[m + 1]; int[] dp = new int[m + 1];
// 状态转移:首行 // 状态转移:首行
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class knapsack { public class knapsack {
/* 0-1 背包:暴力搜索 */ /* 0-1 背包:暴力搜索 */
public int KnapsackDFS(int[] weight, int[] val, int i, int c) { int KnapsackDFS(int[] weight, int[] val, int i, int c) {
// 若已选完所有物品或背包无容量,则返回价值 0 // 若已选完所有物品或背包无容量,则返回价值 0
if (i == 0 || c == 0) { if (i == 0 || c == 0) {
return 0; return 0;
@@ -25,7 +25,7 @@ public class knapsack {
} }
/* 0-1 背包:记忆化搜索 */ /* 0-1 背包:记忆化搜索 */
public int KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) { int KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {
// 若已选完所有物品或背包无容量,则返回价值 0 // 若已选完所有物品或背包无容量,则返回价值 0
if (i == 0 || c == 0) { if (i == 0 || c == 0) {
return 0; return 0;
@@ -47,7 +47,7 @@ public class knapsack {
} }
/* 0-1 背包:动态规划 */ /* 0-1 背包:动态规划 */
public int KnapsackDP(int[] weight, int[] val, int cap) { int KnapsackDP(int[] weight, int[] val, int cap) {
int n = weight.Length; int n = weight.Length;
// 初始化 dp 表 // 初始化 dp 表
int[,] dp = new int[n + 1, cap + 1]; int[,] dp = new int[n + 1, cap + 1];
@@ -67,7 +67,7 @@ public class knapsack {
} }
/* 0-1 背包:空间优化后的动态规划 */ /* 0-1 背包:空间优化后的动态规划 */
public int KnapsackDPComp(int[] weight, int[] val, int cap) { int KnapsackDPComp(int[] weight, int[] val, int cap) {
int n = weight.Length; int n = weight.Length;
// 初始化 dp 表 // 初始化 dp 表
int[] dp = new int[cap + 1]; int[] dp = new int[cap + 1];
@@ -89,8 +89,8 @@ public class knapsack {
[Test] [Test]
public void Test() { public void Test() {
int[] weight = { 10, 20, 30, 40, 50 }; int[] weight = [10, 20, 30, 40, 50];
int[] val = { 50, 120, 150, 210, 240 }; int[] val = [50, 120, 150, 210, 240];
int cap = 50; int cap = 50;
int n = weight.Length; int n = weight.Length;
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class min_cost_climbing_stairs_dp { public class min_cost_climbing_stairs_dp {
/* 爬楼梯最小代价:动态规划 */ /* 爬楼梯最小代价:动态规划 */
public int MinCostClimbingStairsDP(int[] cost) { int MinCostClimbingStairsDP(int[] cost) {
int n = cost.Length - 1; int n = cost.Length - 1;
if (n == 1 || n == 2) if (n == 1 || n == 2)
return cost[n]; return cost[n];
@@ -25,7 +25,7 @@ public class min_cost_climbing_stairs_dp {
} }
/* 爬楼梯最小代价:空间优化后的动态规划 */ /* 爬楼梯最小代价:空间优化后的动态规划 */
public int MinCostClimbingStairsDPComp(int[] cost) { int MinCostClimbingStairsDPComp(int[] cost) {
int n = cost.Length - 1; int n = cost.Length - 1;
if (n == 1 || n == 2) if (n == 1 || n == 2)
return cost[n]; return cost[n];
@@ -40,7 +40,7 @@ public class min_cost_climbing_stairs_dp {
[Test] [Test]
public void Test() { public void Test() {
int[] cost = { 0, 1, 10, 1, 1, 1, 10, 1, 1, 10, 1 }; int[] cost = [0, 1, 10, 1, 1, 1, 10, 1, 1, 10, 1];
Console.WriteLine("输入楼梯的代价列表为"); Console.WriteLine("输入楼梯的代价列表为");
PrintUtil.PrintList(cost); PrintUtil.PrintList(cost);
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class min_path_sum { public class min_path_sum {
/* 最小路径和:暴力搜索 */ /* 最小路径和:暴力搜索 */
public int MinPathSumDFS(int[][] grid, int i, int j) { int MinPathSumDFS(int[][] grid, int i, int j) {
// 若为左上角单元格,则终止搜索 // 若为左上角单元格,则终止搜索
if (i == 0 && j == 0) { if (i == 0 && j == 0) {
return grid[0][0]; return grid[0][0];
@@ -25,7 +25,7 @@ public class min_path_sum {
} }
/* 最小路径和:记忆化搜索 */ /* 最小路径和:记忆化搜索 */
public int MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) { int MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {
// 若为左上角单元格,则终止搜索 // 若为左上角单元格,则终止搜索
if (i == 0 && j == 0) { if (i == 0 && j == 0) {
return grid[0][0]; return grid[0][0];
@@ -47,7 +47,7 @@ public class min_path_sum {
} }
/* 最小路径和:动态规划 */ /* 最小路径和:动态规划 */
public int MinPathSumDP(int[][] grid) { int MinPathSumDP(int[][] grid) {
int n = grid.Length, m = grid[0].Length; int n = grid.Length, m = grid[0].Length;
// 初始化 dp 表 // 初始化 dp 表
int[,] dp = new int[n, m]; int[,] dp = new int[n, m];
@@ -70,7 +70,7 @@ public class min_path_sum {
} }
/* 最小路径和:空间优化后的动态规划 */ /* 最小路径和:空间优化后的动态规划 */
public int MinPathSumDPComp(int[][] grid) { int MinPathSumDPComp(int[][] grid) {
int n = grid.Length, m = grid[0].Length; int n = grid.Length, m = grid[0].Length;
// 初始化 dp 表 // 初始化 dp 表
int[] dp = new int[m]; int[] dp = new int[m];
@@ -94,12 +94,12 @@ public class min_path_sum {
[Test] [Test]
public void Test() { public void Test() {
int[][] grid = int[][] grid =
{ [
new int[4] { 1, 3, 1, 5 }, [1, 3, 1, 5],
new int[4] { 2, 2, 4, 2 }, [2, 2, 4, 2],
new int[4] { 5, 3, 2, 1 }, [5, 3, 2, 1],
new int[4] { 4, 3, 5, 2 } [4, 3, 5, 2]
}; ];
int n = grid.Length, m = grid[0].Length; int n = grid.Length, m = grid[0].Length;
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class unbounded_knapsack { public class unbounded_knapsack {
/* 完全背包:动态规划 */ /* 完全背包:动态规划 */
public int UnboundedKnapsackDP(int[] wgt, int[] val, int cap) { int UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {
int n = wgt.Length; int n = wgt.Length;
// 初始化 dp 表 // 初始化 dp 表
int[,] dp = new int[n + 1, cap + 1]; int[,] dp = new int[n + 1, cap + 1];
@@ -28,7 +28,7 @@ public class unbounded_knapsack {
} }
/* 完全背包:空间优化后的动态规划 */ /* 完全背包:空间优化后的动态规划 */
public int UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) { int UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {
int n = wgt.Length; int n = wgt.Length;
// 初始化 dp 表 // 初始化 dp 表
int[] dp = new int[cap + 1]; int[] dp = new int[cap + 1];
@@ -49,8 +49,8 @@ public class unbounded_knapsack {
[Test] [Test]
public void Test() { public void Test() {
int[] wgt = { 1, 2, 3 }; int[] wgt = [1, 2, 3];
int[] val = { 5, 11, 15 }; int[] val = [5, 11, 15];
int cap = 4; int cap = 4;
// 动态规划 // 动态规划
@@ -13,7 +13,7 @@ public class GraphAdjList {
/* 构造函数 */ /* 构造函数 */
public GraphAdjList(Vertex[][] edges) { public GraphAdjList(Vertex[][] edges) {
this.adjList = new Dictionary<Vertex, List<Vertex>>(); adjList = [];
// 添加所有顶点和边 // 添加所有顶点和边
foreach (Vertex[] edge in edges) { foreach (Vertex[] edge in edges) {
AddVertex(edge[0]); AddVertex(edge[0]);
@@ -23,7 +23,7 @@ public class GraphAdjList {
} }
/* 获取顶点数量 */ /* 获取顶点数量 */
public int Size() { int Size() {
return adjList.Count; return adjList.Count;
} }
@@ -50,7 +50,7 @@ public class GraphAdjList {
if (adjList.ContainsKey(vet)) if (adjList.ContainsKey(vet))
return; return;
// 在邻接表中添加一个新链表 // 在邻接表中添加一个新链表
adjList.Add(vet, new List<Vertex>()); adjList.Add(vet, []);
} }
/* 删除顶点 */ /* 删除顶点 */
@@ -69,7 +69,7 @@ public class GraphAdjList {
public void Print() { public void Print() {
Console.WriteLine("邻接表 ="); Console.WriteLine("邻接表 =");
foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) { foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {
List<int> tmp = new(); List<int> tmp = [];
foreach (Vertex vertex in pair.Value) foreach (Vertex vertex in pair.Value)
tmp.Add(vertex.val); tmp.Add(vertex.val);
Console.WriteLine(pair.Key.val + ": [" + string.Join(", ", tmp) + "],"); Console.WriteLine(pair.Key.val + ": [" + string.Join(", ", tmp) + "],");
@@ -81,10 +81,16 @@ public class graph_adjacency_list {
[Test] [Test]
public void Test() { public void Test() {
/* 初始化无向图 */ /* 初始化无向图 */
Vertex[] v = Vertex.ValsToVets(new int[] { 1, 3, 2, 5, 4 }); Vertex[] v = Vertex.ValsToVets([1, 3, 2, 5, 4]);
Vertex[][] edges = new Vertex[][] { new Vertex[] { v[0], v[1] }, new Vertex[] { v[0], v[3] }, Vertex[][] edges =
new Vertex[] { v[1], v[2] }, new Vertex[] { v[2], v[3] }, [
new Vertex[] { v[2], v[4] }, new Vertex[] { v[3], v[4] } }; [v[0], v[1]],
[v[0], v[3]],
[v[1], v[2]],
[v[2], v[3]],
[v[2], v[4]],
[v[3], v[4]]
];
GraphAdjList graph = new(edges); GraphAdjList graph = new(edges);
Console.WriteLine("\n初始化后,图为"); Console.WriteLine("\n初始化后,图为");
graph.Print(); graph.Print();
@@ -8,13 +8,13 @@ namespace hello_algo.chapter_graph;
/* 基于邻接矩阵实现的无向图类 */ /* 基于邻接矩阵实现的无向图类 */
class GraphAdjMat { class GraphAdjMat {
readonly List<int> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引” List<int> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
readonly List<List<int>> adjMat; // 邻接矩阵,行列索引对应“顶点索引” List<List<int>> adjMat; // 邻接矩阵,行列索引对应“顶点索引”
/* 构造函数 */ /* 构造函数 */
public GraphAdjMat(int[] vertices, int[][] edges) { public GraphAdjMat(int[] vertices, int[][] edges) {
this.vertices = new List<int>(); this.vertices = [];
this.adjMat = new List<List<int>>(); this.adjMat = [];
// 添加顶点 // 添加顶点
foreach (int val in vertices) { foreach (int val in vertices) {
AddVertex(val); AddVertex(val);
@@ -27,7 +27,7 @@ class GraphAdjMat {
} }
/* 获取顶点数量 */ /* 获取顶点数量 */
public int Size() { int Size() {
return vertices.Count; return vertices.Count;
} }
@@ -97,10 +97,16 @@ public class graph_adjacency_matrix {
public void Test() { public void Test() {
/* 初始化无向图 */ /* 初始化无向图 */
// 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引 // 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引
int[] vertices = { 1, 3, 2, 5, 4 }; int[] vertices = [1, 3, 2, 5, 4];
int[][] edges = new int[][] { new int[] { 0, 1 }, new int[] { 0, 3 }, int[][] edges =
new int[] { 1, 2 }, new int[] { 2, 3 }, [
new int[] { 2, 4 }, new int[] { 3, 4 } }; [0, 1],
[0, 3],
[1, 2],
[2, 3],
[2, 4],
[3, 4]
];
GraphAdjMat graph = new(vertices, edges); GraphAdjMat graph = new(vertices, edges);
Console.WriteLine("\n初始化后,图为"); Console.WriteLine("\n初始化后,图为");
graph.Print(); graph.Print();
+11 -11
View File
@@ -9,11 +9,11 @@ namespace hello_algo.chapter_graph;
public class graph_bfs { public class graph_bfs {
/* 广度优先遍历 BFS */ /* 广度优先遍历 BFS */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点 // 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
public static List<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) { List<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {
// 顶点遍历序列 // 顶点遍历序列
List<Vertex> res = new(); List<Vertex> res = [];
// 哈希表,用于记录已被访问过的顶点 // 哈希表,用于记录已被访问过的顶点
HashSet<Vertex> visited = new() { startVet }; HashSet<Vertex> visited = [startVet];
// 队列用于实现 BFS // 队列用于实现 BFS
Queue<Vertex> que = new(); Queue<Vertex> que = new();
que.Enqueue(startVet); que.Enqueue(startVet);
@@ -37,14 +37,14 @@ public class graph_bfs {
[Test] [Test]
public void Test() { public void Test() {
/* 初始化无向图 */ /* 初始化无向图 */
Vertex[] v = Vertex.ValsToVets(new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }); Vertex[] v = Vertex.ValsToVets([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
Vertex[][] edges = new Vertex[12][] Vertex[][] edges =
{ [
new Vertex[2] { v[0], v[1] }, new Vertex[2] { v[0], v[3] }, new Vertex[2] { v[1], v[2] }, [v[0], v[1]], [v[0], v[3]], [v[1], v[2]],
new Vertex[2] { v[1], v[4] }, new Vertex[2] { v[2], v[5] }, new Vertex[2] { v[3], v[4] }, [v[1], v[4]], [v[2], v[5]], [v[3], v[4]],
new Vertex[2] { v[3], v[6] }, new Vertex[2] { v[4], v[5] }, new Vertex[2] { v[4], v[7] }, [v[3], v[6]], [v[4], v[5]], [v[4], v[7]],
new Vertex[2] { v[5], v[8] }, new Vertex[2] { v[6], v[7] }, new Vertex[2] { v[7], v[8] } [v[5], v[8]], [v[6], v[7]], [v[7], v[8]]
}; ];
GraphAdjList graph = new(edges); GraphAdjList graph = new(edges);
Console.WriteLine("\n初始化后,图为"); Console.WriteLine("\n初始化后,图为");
+10 -10
View File
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_graph;
public class graph_dfs { public class graph_dfs {
/* 深度优先遍历 DFS 辅助函数 */ /* 深度优先遍历 DFS 辅助函数 */
public void DFS(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) { void DFS(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) {
res.Add(vet); // 记录访问顶点 res.Add(vet); // 记录访问顶点
visited.Add(vet); // 标记该顶点已被访问 visited.Add(vet); // 标记该顶点已被访问
// 遍历该顶点的所有邻接顶点 // 遍历该顶点的所有邻接顶点
@@ -23,11 +23,11 @@ public class graph_dfs {
/* 深度优先遍历 DFS */ /* 深度优先遍历 DFS */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点 // 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
public List<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) { List<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {
// 顶点遍历序列 // 顶点遍历序列
List<Vertex> res = new(); List<Vertex> res = [];
// 哈希表,用于记录已被访问过的顶点 // 哈希表,用于记录已被访问过的顶点
HashSet<Vertex> visited = new(); HashSet<Vertex> visited = [];
DFS(graph, visited, res, startVet); DFS(graph, visited, res, startVet);
return res; return res;
} }
@@ -35,12 +35,12 @@ public class graph_dfs {
[Test] [Test]
public void Test() { public void Test() {
/* 初始化无向图 */ /* 初始化无向图 */
Vertex[] v = Vertex.ValsToVets(new int[7] { 0, 1, 2, 3, 4, 5, 6 }); Vertex[] v = Vertex.ValsToVets([0, 1, 2, 3, 4, 5, 6]);
Vertex[][] edges = new Vertex[6][] Vertex[][] edges =
{ [
new Vertex[2] { v[0], v[1] }, new Vertex[2] { v[0], v[3] }, new Vertex[2] { v[1], v[2] }, [v[0], v[1]], [v[0], v[3]], [v[1], v[2]],
new Vertex[2] { v[2], v[5] }, new Vertex[2] { v[4], v[5] }, new Vertex[2] { v[5], v[6] }, [v[2], v[5]], [v[4], v[5]], [v[5], v[6]],
}; ];
GraphAdjList graph = new(edges); GraphAdjList graph = new(edges);
Console.WriteLine("\n初始化后,图为"); Console.WriteLine("\n初始化后,图为");
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_greedy;
public class coin_change_greedy { public class coin_change_greedy {
/* 零钱兑换:贪心 */ /* 零钱兑换:贪心 */
public int CoinChangeGreedy(int[] coins, int amt) { int CoinChangeGreedy(int[] coins, int amt) {
// 假设 coins 列表有序 // 假设 coins 列表有序
int i = coins.Length - 1; int i = coins.Length - 1;
int count = 0; int count = 0;
@@ -29,14 +29,14 @@ public class coin_change_greedy {
[Test] [Test]
public void Test() { public void Test() {
// 贪心:能够保证找到全局最优解 // 贪心:能够保证找到全局最优解
int[] coins = { 1, 5, 10, 20, 50, 100 }; int[] coins = [1, 5, 10, 20, 50, 100];
int amt = 186; int amt = 186;
int res = CoinChangeGreedy(coins, amt); int res = CoinChangeGreedy(coins, amt);
Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt); Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt);
Console.WriteLine("凑到 " + amt + " 所需的最少硬币数量为 " + res); Console.WriteLine("凑到 " + amt + " 所需的最少硬币数量为 " + res);
// 贪心:无法保证找到全局最优解 // 贪心:无法保证找到全局最优解
coins = new int[] { 1, 20, 50 }; coins = [1, 20, 50];
amt = 60; amt = 60;
res = CoinChangeGreedy(coins, amt); res = CoinChangeGreedy(coins, amt);
Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt); Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt);
@@ -44,7 +44,7 @@ public class coin_change_greedy {
Console.WriteLine("实际上需要的最少数量为 3 ,即 20 + 20 + 20"); Console.WriteLine("实际上需要的最少数量为 3 ,即 20 + 20 + 20");
// 贪心:无法保证找到全局最优解 // 贪心:无法保证找到全局最优解
coins = new int[] { 1, 49, 50 }; coins = [1, 49, 50];
amt = 98; amt = 98;
res = CoinChangeGreedy(coins, amt); res = CoinChangeGreedy(coins, amt);
Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt); Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt);
@@ -7,19 +7,14 @@
namespace hello_algo.chapter_greedy; namespace hello_algo.chapter_greedy;
/* 物品 */ /* 物品 */
class Item { class Item(int w, int v) {
public int w; // 物品重量 public int w = w; // 物品重量
public int v; // 物品价值 public int v = v; // 物品价值
public Item(int w, int v) {
this.w = w;
this.v = v;
}
} }
public class fractional_knapsack { public class fractional_knapsack {
/* 分数背包:贪心 */ /* 分数背包:贪心 */
public double FractionalKnapsack(int[] wgt, int[] val, int cap) { double FractionalKnapsack(int[] wgt, int[] val, int cap) {
// 创建物品列表,包含两个属性:重量、价值 // 创建物品列表,包含两个属性:重量、价值
Item[] items = new Item[wgt.Length]; Item[] items = new Item[wgt.Length];
for (int i = 0; i < wgt.Length; i++) { for (int i = 0; i < wgt.Length; i++) {
@@ -46,8 +41,8 @@ public class fractional_knapsack {
[Test] [Test]
public void Test() { public void Test() {
int[] wgt = { 10, 20, 30, 40, 50 }; int[] wgt = [10, 20, 30, 40, 50];
int[] val = { 50, 120, 150, 210, 240 }; int[] val = [50, 120, 150, 210, 240];
int cap = 50; int cap = 50;
// 贪心算法 // 贪心算法
+2 -2
View File
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_greedy;
public class max_capacity { public class max_capacity {
/* 最大容量:贪心 */ /* 最大容量:贪心 */
public int MaxCapacity(int[] ht) { int MaxCapacity(int[] ht) {
// 初始化 i, j 分列数组两端 // 初始化 i, j 分列数组两端
int i = 0, j = ht.Length - 1; int i = 0, j = ht.Length - 1;
// 初始最大容量为 0 // 初始最大容量为 0
@@ -30,7 +30,7 @@ public class max_capacity {
[Test] [Test]
public void Test() { public void Test() {
int[] ht = { 3, 8, 5, 2, 7, 7, 3, 4 }; int[] ht = [3, 8, 5, 2, 7, 7, 3, 4];
// 贪心算法 // 贪心算法
int res = MaxCapacity(ht); int res = MaxCapacity(ht);
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_greedy;
public class max_product_cutting { public class max_product_cutting {
/* 最大切分乘积:贪心 */ /* 最大切分乘积:贪心 */
public int MaxProductCutting(int n) { int MaxProductCutting(int n) {
// 当 n <= 3 时,必须切分出一个 1 // 当 n <= 3 时,必须切分出一个 1
if (n <= 3) { if (n <= 3) {
return 1 * (n - 1); return 1 * (n - 1);
+9 -13
View File
@@ -7,28 +7,24 @@
namespace hello_algo.chapter_hashing; namespace hello_algo.chapter_hashing;
/* 键值对 int->string */ /* 键值对 int->string */
class Pair { class Pair(int key, string val) {
public int key; public int key = key;
public string val; public string val = val;
public Pair(int key, string val) {
this.key = key;
this.val = val;
}
} }
/* 基于数组简易实现的哈希表 */ /* 基于数组简易实现的哈希表 */
class ArrayHashMap { class ArrayHashMap {
private readonly List<Pair?> buckets; List<Pair?> buckets;
public ArrayHashMap() { public ArrayHashMap() {
// 初始化数组,包含 100 个桶 // 初始化数组,包含 100 个桶
buckets = new(); buckets = [];
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
buckets.Add(null); buckets.Add(null);
} }
} }
/* 哈希函数 */ /* 哈希函数 */
private int HashFunc(int key) { int HashFunc(int key) {
int index = key % 100; int index = key % 100;
return index; return index;
} }
@@ -57,7 +53,7 @@ class ArrayHashMap {
/* 获取所有键值对 */ /* 获取所有键值对 */
public List<Pair> PairSet() { public List<Pair> PairSet() {
List<Pair> pairSet = new(); List<Pair> pairSet = [];
foreach (Pair? pair in buckets) { foreach (Pair? pair in buckets) {
if (pair != null) if (pair != null)
pairSet.Add(pair); pairSet.Add(pair);
@@ -67,7 +63,7 @@ class ArrayHashMap {
/* 获取所有键 */ /* 获取所有键 */
public List<int> KeySet() { public List<int> KeySet() {
List<int> keySet = new(); List<int> keySet = [];
foreach (Pair? pair in buckets) { foreach (Pair? pair in buckets) {
if (pair != null) if (pair != null)
keySet.Add(pair.key); keySet.Add(pair.key);
@@ -77,7 +73,7 @@ class ArrayHashMap {
/* 获取所有值 */ /* 获取所有值 */
public List<string> ValueSet() { public List<string> ValueSet() {
List<string> valueSet = new(); List<string> valueSet = [];
foreach (Pair? pair in buckets) { foreach (Pair? pair in buckets) {
if (pair != null) if (pair != null)
valueSet.Add(pair.val); valueSet.Add(pair.val);
@@ -25,7 +25,7 @@ public class built_in_hash {
int hashStr = str.GetHashCode(); int hashStr = str.GetHashCode();
Console.WriteLine("字符串 " + str + " 的哈希值为 " + hashStr); Console.WriteLine("字符串 " + str + " 的哈希值为 " + hashStr);
object[] arr = { 12836, "小哈" }; object[] arr = [12836, "小哈"];
int hashTup = arr.GetHashCode(); int hashTup = arr.GetHashCode();
Console.WriteLine("数组 [" + string.Join(", ", arr) + "] 的哈希值为 " + hashTup); Console.WriteLine("数组 [" + string.Join(", ", arr) + "] 的哈希值为 " + hashTup);
@@ -10,8 +10,8 @@ namespace hello_algo.chapter_hashing;
class HashMapChaining { class HashMapChaining {
int size; // 键值对数量 int size; // 键值对数量
int capacity; // 哈希表容量 int capacity; // 哈希表容量
readonly double loadThres; // 触发扩容的负载因子阈值 double loadThres; // 触发扩容的负载因子阈值
readonly int extendRatio; // 扩容倍数 int extendRatio; // 扩容倍数
List<List<Pair>> buckets; // 桶数组 List<List<Pair>> buckets; // 桶数组
/* 构造方法 */ /* 构造方法 */
@@ -22,17 +22,17 @@ class HashMapChaining {
extendRatio = 2; extendRatio = 2;
buckets = new List<List<Pair>>(capacity); buckets = new List<List<Pair>>(capacity);
for (int i = 0; i < capacity; i++) { for (int i = 0; i < capacity; i++) {
buckets.Add(new List<Pair>()); buckets.Add([]);
} }
} }
/* 哈希函数 */ /* 哈希函数 */
private int HashFunc(int key) { int HashFunc(int key) {
return key % capacity; return key % capacity;
} }
/* 负载因子 */ /* 负载因子 */
private double LoadFactor() { double LoadFactor() {
return (double)size / capacity; return (double)size / capacity;
} }
@@ -82,14 +82,14 @@ class HashMapChaining {
} }
/* 扩容哈希表 */ /* 扩容哈希表 */
private void Extend() { void Extend() {
// 暂存原哈希表 // 暂存原哈希表
List<List<Pair>> bucketsTmp = buckets; List<List<Pair>> bucketsTmp = buckets;
// 初始化扩容后的新哈希表 // 初始化扩容后的新哈希表
capacity *= extendRatio; capacity *= extendRatio;
buckets = new List<List<Pair>>(capacity); buckets = new List<List<Pair>>(capacity);
for (int i = 0; i < capacity; i++) { for (int i = 0; i < capacity; i++) {
buckets.Add(new List<Pair>()); buckets.Add([]);
} }
size = 0; size = 0;
// 将键值对从原哈希表搬运至新哈希表 // 将键值对从原哈希表搬运至新哈希表
@@ -103,7 +103,7 @@ class HashMapChaining {
/* 打印哈希表 */ /* 打印哈希表 */
public void Print() { public void Print() {
foreach (List<Pair> bucket in buckets) { foreach (List<Pair> bucket in buckets) {
List<string> res = new(); List<string> res = [];
foreach (Pair pair in bucket) { foreach (Pair pair in bucket) {
res.Add(pair.key + " -> " + pair.val); res.Add(pair.key + " -> " + pair.val);
} }
@@ -8,12 +8,12 @@ namespace hello_algo.chapter_hashing;
/* 开放寻址哈希表 */ /* 开放寻址哈希表 */
class HashMapOpenAddressing { class HashMapOpenAddressing {
private int size; // 键值对数量 int size; // 键值对数量
private int capacity = 4; // 哈希表容量 int capacity = 4; // 哈希表容量
private readonly double loadThres = 2.0 / 3.0; // 触发扩容的负载因子阈值 double loadThres = 2.0 / 3.0; // 触发扩容的负载因子阈值
private readonly int extendRatio = 2; // 扩容倍数 int extendRatio = 2; // 扩容倍数
private Pair[] buckets; // 桶数组 Pair[] buckets; // 桶数组
private readonly Pair TOMBSTONE = new(-1, "-1"); // 删除标记 Pair TOMBSTONE = new(-1, "-1"); // 删除标记
/* 构造方法 */ /* 构造方法 */
public HashMapOpenAddressing() { public HashMapOpenAddressing() {
@@ -22,17 +22,17 @@ class HashMapOpenAddressing {
} }
/* 哈希函数 */ /* 哈希函数 */
private int HashFunc(int key) { int HashFunc(int key) {
return key % capacity; return key % capacity;
} }
/* 负载因子 */ /* 负载因子 */
private double LoadFactor() { double LoadFactor() {
return (double)size / capacity; return (double)size / capacity;
} }
/* 搜索 key 对应的桶索引 */ /* 搜索 key 对应的桶索引 */
private int FindBucket(int key) { int FindBucket(int key) {
int index = HashFunc(key); int index = HashFunc(key);
int firstTombstone = -1; int firstTombstone = -1;
// 线性探测,当遇到空桶时跳出 // 线性探测,当遇到空桶时跳出
@@ -100,7 +100,7 @@ class HashMapOpenAddressing {
} }
/* 扩容哈希表 */ /* 扩容哈希表 */
private void Extend() { void Extend() {
// 暂存原哈希表 // 暂存原哈希表
Pair[] bucketsTmp = buckets; Pair[] bucketsTmp = buckets;
// 初始化扩容后的新哈希表 // 初始化扩容后的新哈希表
+4 -4
View File
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_hashing;
public class simple_hash { public class simple_hash {
/* 加法哈希 */ /* 加法哈希 */
public static int AddHash(string key) { int AddHash(string key) {
long hash = 0; long hash = 0;
const int MODULUS = 1000000007; const int MODULUS = 1000000007;
foreach (char c in key) { foreach (char c in key) {
@@ -18,7 +18,7 @@ public class simple_hash {
} }
/* 乘法哈希 */ /* 乘法哈希 */
public static int MulHash(string key) { int MulHash(string key) {
long hash = 0; long hash = 0;
const int MODULUS = 1000000007; const int MODULUS = 1000000007;
foreach (char c in key) { foreach (char c in key) {
@@ -28,7 +28,7 @@ public class simple_hash {
} }
/* 异或哈希 */ /* 异或哈希 */
public static int XorHash(string key) { int XorHash(string key) {
int hash = 0; int hash = 0;
const int MODULUS = 1000000007; const int MODULUS = 1000000007;
foreach (char c in key) { foreach (char c in key) {
@@ -38,7 +38,7 @@ public class simple_hash {
} }
/* 旋转哈希 */ /* 旋转哈希 */
public static int RotHash(string key) { int RotHash(string key) {
long hash = 0; long hash = 0;
const int MODULUS = 1000000007; const int MODULUS = 1000000007;
foreach (char c in key) { foreach (char c in key) {
+2 -2
View File
@@ -7,13 +7,13 @@
namespace hello_algo.chapter_heap; namespace hello_algo.chapter_heap;
public class heap { public class heap {
public void TestPush(PriorityQueue<int, int> heap, int val) { void TestPush(PriorityQueue<int, int> heap, int val) {
heap.Enqueue(val, val); // 元素入堆 heap.Enqueue(val, val); // 元素入堆
Console.WriteLine($"\n元素 {val} 入堆后\n"); Console.WriteLine($"\n元素 {val} 入堆后\n");
PrintUtil.PrintHeap(heap); PrintUtil.PrintHeap(heap);
} }
public void TestPop(PriorityQueue<int, int> heap) { void TestPop(PriorityQueue<int, int> heap) {
int val = heap.Dequeue(); // 堆顶元素出堆 int val = heap.Dequeue(); // 堆顶元素出堆
Console.WriteLine($"\n堆顶元素 {val} 出堆后\n"); Console.WriteLine($"\n堆顶元素 {val} 出堆后\n");
PrintUtil.PrintHeap(heap); PrintUtil.PrintHeap(heap);
+3 -3
View File
@@ -9,11 +9,11 @@ namespace hello_algo.chapter_heap;
/* 大顶堆 */ /* 大顶堆 */
class MaxHeap { class MaxHeap {
// 使用列表而非数组,这样无须考虑扩容问题 // 使用列表而非数组,这样无须考虑扩容问题
private readonly List<int> maxHeap; List<int> maxHeap;
/* 构造函数,建立空堆 */ /* 构造函数,建立空堆 */
public MaxHeap() { public MaxHeap() {
maxHeap = new List<int>(); maxHeap = [];
} }
/* 构造函数,根据输入列表建堆 */ /* 构造函数,根据输入列表建堆 */
@@ -130,7 +130,7 @@ public class my_heap {
[Test] [Test]
public void Test() { public void Test() {
/* 初始化大顶堆 */ /* 初始化大顶堆 */
MaxHeap maxHeap = new(new int[] { 9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2 }); MaxHeap maxHeap = new([9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2]);
Console.WriteLine("\n输入列表并建堆后"); Console.WriteLine("\n输入列表并建堆后");
maxHeap.Print(); maxHeap.Print();
+2 -2
View File
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_heap;
public class top_k { public class top_k {
/* 基于堆查找数组中最大的 k 个元素 */ /* 基于堆查找数组中最大的 k 个元素 */
public static PriorityQueue<int, int> TopKHeap(int[] nums, int k) { PriorityQueue<int, int> TopKHeap(int[] nums, int k) {
// 初始化小顶堆 // 初始化小顶堆
PriorityQueue<int, int> heap = new(); PriorityQueue<int, int> heap = new();
// 将数组的前 k 个元素入堆 // 将数组的前 k 个元素入堆
@@ -28,7 +28,7 @@ public class top_k {
[Test] [Test]
public void Test() { public void Test() {
int[] nums = { 1, 7, 6, 3, 2 }; int[] nums = [1, 7, 6, 3, 2];
int k = 3; int k = 3;
PriorityQueue<int, int> res = TopKHeap(nums, k); PriorityQueue<int, int> res = TopKHeap(nums, k);
Console.WriteLine("最大的 " + k + " 个元素为"); Console.WriteLine("最大的 " + k + " 个元素为");
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_searching;
public class binary_search { public class binary_search {
/* 二分查找(双闭区间) */ /* 二分查找(双闭区间) */
static int BinarySearch(int[] nums, int target) { int BinarySearch(int[] nums, int target) {
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素 // 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
int i = 0, j = nums.Length - 1; int i = 0, j = nums.Length - 1;
// 循环,当搜索区间为空时跳出(当 i > j 时为空) // 循环,当搜索区间为空时跳出(当 i > j 时为空)
@@ -26,7 +26,7 @@ public class binary_search {
} }
/* 二分查找(左闭右开) */ /* 二分查找(左闭右开) */
static int BinarySearchLCRO(int[] nums, int target) { int BinarySearchLCRO(int[] nums, int target) {
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
int i = 0, j = nums.Length; int i = 0, j = nums.Length;
// 循环,当搜索区间为空时跳出(当 i = j 时为空) // 循环,当搜索区间为空时跳出(当 i = j 时为空)
@@ -46,7 +46,7 @@ public class binary_search {
[Test] [Test]
public void Test() { public void Test() {
int target = 6; int target = 6;
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 }; int[] nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
/* 二分查找(双闭区间) */ /* 二分查找(双闭区间) */
int index = BinarySearch(nums, target); int index = BinarySearch(nums, target);
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_searching;
public class binary_search_edge { public class binary_search_edge {
/* 二分查找最左一个 target */ /* 二分查找最左一个 target */
public int BinarySearchLeftEdge(int[] nums, int target) { int BinarySearchLeftEdge(int[] nums, int target) {
// 等价于查找 target 的插入点 // 等价于查找 target 的插入点
int i = binary_search_insertion.BinarySearchInsertion(nums, target); int i = binary_search_insertion.BinarySearchInsertion(nums, target);
// 未找到 target ,返回 -1 // 未找到 target ,返回 -1
@@ -20,7 +20,7 @@ public class binary_search_edge {
} }
/* 二分查找最右一个 target */ /* 二分查找最右一个 target */
public int BinarySearchRightEdge(int[] nums, int target) { int BinarySearchRightEdge(int[] nums, int target) {
// 转化为查找最左一个 target + 1 // 转化为查找最左一个 target + 1
int i = binary_search_insertion.BinarySearchInsertion(nums, target + 1); int i = binary_search_insertion.BinarySearchInsertion(nums, target + 1);
// j 指向最右一个 target ,i 指向首个大于 target 的元素 // j 指向最右一个 target ,i 指向首个大于 target 的元素
@@ -36,7 +36,7 @@ public class binary_search_edge {
[Test] [Test]
public void Test() { public void Test() {
// 包含重复元素的数组 // 包含重复元素的数组
int[] nums = { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 }; int[] nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
Console.WriteLine("\n数组 nums = " + nums.PrintList()); Console.WriteLine("\n数组 nums = " + nums.PrintList());
// 二分查找左边界和右边界 // 二分查找左边界和右边界
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_searching;
public class binary_search_insertion { public class binary_search_insertion {
/* 二分查找插入点(无重复元素) */ /* 二分查找插入点(无重复元素) */
public int BinarySearchInsertionSimple(int[] nums, int target) { public static int BinarySearchInsertionSimple(int[] nums, int target) {
int i = 0, j = nums.Length - 1; // 初始化双闭区间 [0, n-1] int i = 0, j = nums.Length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) { while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m int m = i + (j - i) / 2; // 计算中点索引 m
@@ -44,7 +44,7 @@ public class binary_search_insertion {
[Test] [Test]
public void Test() { public void Test() {
// 无重复元素的数组 // 无重复元素的数组
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 }; int[] nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
Console.WriteLine("\n数组 nums = " + nums.PrintList()); Console.WriteLine("\n数组 nums = " + nums.PrintList());
// 二分查找插入点 // 二分查找插入点
foreach (int target in new int[] { 6, 9 }) { foreach (int target in new int[] { 6, 9 }) {
@@ -53,7 +53,7 @@ public class binary_search_insertion {
} }
// 包含重复元素的数组 // 包含重复元素的数组
nums = new int[] { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 }; nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
Console.WriteLine("\n数组 nums = " + nums.PrintList()); Console.WriteLine("\n数组 nums = " + nums.PrintList());
// 二分查找插入点 // 二分查找插入点
foreach (int target in new int[] { 2, 6, 20 }) { foreach (int target in new int[] { 2, 6, 20 }) {
@@ -8,14 +8,14 @@ namespace hello_algo.chapter_searching;
public class hashing_search { public class hashing_search {
/* 哈希查找(数组) */ /* 哈希查找(数组) */
static int HashingSearchArray(Dictionary<int, int> map, int target) { int HashingSearchArray(Dictionary<int, int> map, int target) {
// 哈希表的 key: 目标元素,value: 索引 // 哈希表的 key: 目标元素,value: 索引
// 若哈希表中无此 key ,返回 -1 // 若哈希表中无此 key ,返回 -1
return map.GetValueOrDefault(target, -1); return map.GetValueOrDefault(target, -1);
} }
/* 哈希查找(链表) */ /* 哈希查找(链表) */
static ListNode? HashingSearchLinkedList(Dictionary<int, ListNode> map, int target) { ListNode? HashingSearchLinkedList(Dictionary<int, ListNode> map, int target) {
// 哈希表的 key: 目标节点值,value: 节点对象 // 哈希表的 key: 目标节点值,value: 节点对象
// 若哈希表中无此 key ,返回 null // 若哈希表中无此 key ,返回 null
@@ -27,9 +27,9 @@ public class hashing_search {
int target = 3; int target = 3;
/* 哈希查找(数组) */ /* 哈希查找(数组) */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 }; int[] nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
// 初始化哈希表 // 初始化哈希表
Dictionary<int, int> map = new(); Dictionary<int, int> map = [];
for (int i = 0; i < nums.Length; i++) { for (int i = 0; i < nums.Length; i++) {
map[nums[i]] = i; // key: 元素,value: 索引 map[nums[i]] = i; // key: 元素,value: 索引
} }
@@ -39,7 +39,7 @@ public class hashing_search {
/* 哈希查找(链表) */ /* 哈希查找(链表) */
ListNode? head = ListNode.ArrToLinkedList(nums); ListNode? head = ListNode.ArrToLinkedList(nums);
// 初始化哈希表 // 初始化哈希表
Dictionary<int, ListNode> map1 = new(); Dictionary<int, ListNode> map1 = [];
while (head != null) { while (head != null) {
map1[head.val] = head; // key: 节点值,value: 节点 map1[head.val] = head; // key: 节点值,value: 节点
head = head.next; head = head.next;
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_searching;
public class linear_search { public class linear_search {
/* 线性查找(数组) */ /* 线性查找(数组) */
static int LinearSearchArray(int[] nums, int target) { int LinearSearchArray(int[] nums, int target) {
// 遍历数组 // 遍历数组
for (int i = 0; i < nums.Length; i++) { for (int i = 0; i < nums.Length; i++) {
// 找到目标元素,返回其索引 // 找到目标元素,返回其索引
@@ -20,7 +20,7 @@ public class linear_search {
} }
/* 线性查找(链表) */ /* 线性查找(链表) */
static ListNode? LinearSearchLinkedList(ListNode? head, int target) { ListNode? LinearSearchLinkedList(ListNode? head, int target) {
// 遍历链表 // 遍历链表
while (head != null) { while (head != null) {
// 找到目标节点,返回之 // 找到目标节点,返回之
@@ -37,7 +37,7 @@ public class linear_search {
int target = 3; int target = 3;
/* 在数组中执行线性查找 */ /* 在数组中执行线性查找 */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 }; int[] nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
int index = LinearSearchArray(nums, target); int index = LinearSearchArray(nums, target);
Console.WriteLine("目标元素 3 的索引 = " + index); Console.WriteLine("目标元素 3 的索引 = " + index);
+8 -8
View File
@@ -8,37 +8,37 @@ namespace hello_algo.chapter_searching;
public class two_sum { public class two_sum {
/* 方法一:暴力枚举 */ /* 方法一:暴力枚举 */
public static int[] TwoSumBruteForce(int[] nums, int target) { int[] TwoSumBruteForce(int[] nums, int target) {
int size = nums.Length; int size = nums.Length;
// 两层循环,时间复杂度 O(n^2) // 两层循环,时间复杂度 O(n^2)
for (int i = 0; i < size - 1; i++) { for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) { for (int j = i + 1; j < size; j++) {
if (nums[i] + nums[j] == target) if (nums[i] + nums[j] == target)
return new int[] { i, j }; return [i, j];
} }
} }
return Array.Empty<int>(); return [];
} }
/* 方法二:辅助哈希表 */ /* 方法二:辅助哈希表 */
public static int[] TwoSumHashTable(int[] nums, int target) { int[] TwoSumHashTable(int[] nums, int target) {
int size = nums.Length; int size = nums.Length;
// 辅助哈希表,空间复杂度 O(n) // 辅助哈希表,空间复杂度 O(n)
Dictionary<int, int> dic = new(); Dictionary<int, int> dic = [];
// 单层循环,时间复杂度 O(n) // 单层循环,时间复杂度 O(n)
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if (dic.ContainsKey(target - nums[i])) { if (dic.ContainsKey(target - nums[i])) {
return new int[] { dic[target - nums[i]], i }; return [dic[target - nums[i]], i];
} }
dic.Add(nums[i], i); dic.Add(nums[i], i);
} }
return Array.Empty<int>(); return [];
} }
[Test] [Test]
public void Test() { public void Test() {
// ======= Test Case ======= // ======= Test Case =======
int[] nums = { 2, 7, 11, 15 }; int[] nums = [2, 7, 11, 15];
int target = 13; int target = 13;
// ====== Driver Code ====== // ====== Driver Code ======
+4 -4
View File
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_sorting;
public class bubble_sort { public class bubble_sort {
/* 冒泡排序 */ /* 冒泡排序 */
static void BubbleSort(int[] nums) { void BubbleSort(int[] nums) {
// 外循环:未排序区间为 [0, i] // 外循环:未排序区间为 [0, i]
for (int i = nums.Length - 1; i > 0; i--) { for (int i = nums.Length - 1; i > 0; i--) {
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
@@ -22,7 +22,7 @@ public class bubble_sort {
} }
/* 冒泡排序(标志优化)*/ /* 冒泡排序(标志优化)*/
static void BubbleSortWithFlag(int[] nums) { void BubbleSortWithFlag(int[] nums) {
// 外循环:未排序区间为 [0, i] // 外循环:未排序区间为 [0, i]
for (int i = nums.Length - 1; i > 0; i--) { for (int i = nums.Length - 1; i > 0; i--) {
bool flag = false; // 初始化标志位 bool flag = false; // 初始化标志位
@@ -40,11 +40,11 @@ public class bubble_sort {
[Test] [Test]
public void Test() { public void Test() {
int[] nums = { 4, 1, 3, 1, 5, 2 }; int[] nums = [4, 1, 3, 1, 5, 2];
BubbleSort(nums); BubbleSort(nums);
Console.WriteLine("冒泡排序完成后 nums = " + string.Join(",", nums)); Console.WriteLine("冒泡排序完成后 nums = " + string.Join(",", nums));
int[] nums1 = { 4, 1, 3, 1, 5, 2 }; int[] nums1 = [4, 1, 3, 1, 5, 2];
BubbleSortWithFlag(nums1); BubbleSortWithFlag(nums1);
Console.WriteLine("冒泡排序完成后 nums1 = " + string.Join(",", nums1)); Console.WriteLine("冒泡排序完成后 nums1 = " + string.Join(",", nums1));
} }
+4 -4
View File
@@ -8,12 +8,12 @@ namespace hello_algo.chapter_sorting;
public class bucket_sort { public class bucket_sort {
/* 桶排序 */ /* 桶排序 */
public static void BucketSort(float[] nums) { void BucketSort(float[] nums) {
// 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素 // 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
int k = nums.Length / 2; int k = nums.Length / 2;
List<List<float>> buckets = new(); List<List<float>> buckets = [];
for (int i = 0; i < k; i++) { for (int i = 0; i < k; i++) {
buckets.Add(new List<float>()); buckets.Add([]);
} }
// 1. 将数组元素分配到各个桶中 // 1. 将数组元素分配到各个桶中
foreach (float num in nums) { foreach (float num in nums) {
@@ -39,7 +39,7 @@ public class bucket_sort {
[Test] [Test]
public void Test() { public void Test() {
// 设输入数据为浮点数,范围为 [0, 1) // 设输入数据为浮点数,范围为 [0, 1)
float[] nums = { 0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.75f, 0.15f, 0.37f }; float[] nums = [0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.75f, 0.15f, 0.37f];
BucketSort(nums); BucketSort(nums);
Console.WriteLine("桶排序完成后 nums = " + string.Join(" ", nums)); Console.WriteLine("桶排序完成后 nums = " + string.Join(" ", nums));
} }
@@ -9,7 +9,7 @@ namespace hello_algo.chapter_sorting;
public class counting_sort { public class counting_sort {
/* 计数排序 */ /* 计数排序 */
// 简单实现,无法用于排序对象 // 简单实现,无法用于排序对象
public static void CountingSortNaive(int[] nums) { void CountingSortNaive(int[] nums) {
// 1. 统计数组最大元素 m // 1. 统计数组最大元素 m
int m = 0; int m = 0;
foreach (int num in nums) { foreach (int num in nums) {
@@ -32,7 +32,7 @@ public class counting_sort {
/* 计数排序 */ /* 计数排序 */
// 完整实现,可排序对象,并且是稳定排序 // 完整实现,可排序对象,并且是稳定排序
static void CountingSort(int[] nums) { void CountingSort(int[] nums) {
// 1. 统计数组最大元素 m // 1. 统计数组最大元素 m
int m = 0; int m = 0;
foreach (int num in nums) { foreach (int num in nums) {
@@ -66,11 +66,11 @@ public class counting_sort {
[Test] [Test]
public void Test() { public void Test() {
int[] nums = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 }; int[] nums = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4];
CountingSortNaive(nums); CountingSortNaive(nums);
Console.WriteLine("计数排序(无法排序对象)完成后 nums = " + string.Join(" ", nums)); Console.WriteLine("计数排序(无法排序对象)完成后 nums = " + string.Join(" ", nums));
int[] nums1 = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 }; int[] nums1 = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4];
CountingSort(nums1); CountingSort(nums1);
Console.WriteLine("计数排序完成后 nums1 = " + string.Join(" ", nums)); Console.WriteLine("计数排序完成后 nums1 = " + string.Join(" ", nums));
} }
+3 -3
View File
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_sorting;
public class heap_sort { public class heap_sort {
/* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */ /* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */
public static void SiftDown(int[] nums, int n, int i) { void SiftDown(int[] nums, int n, int i) {
while (true) { while (true) {
// 判断节点 i, l, r 中值最大的节点,记为 ma // 判断节点 i, l, r 中值最大的节点,记为 ma
int l = 2 * i + 1; int l = 2 * i + 1;
@@ -29,7 +29,7 @@ public class heap_sort {
} }
/* 堆排序 */ /* 堆排序 */
public static void HeapSort(int[] nums) { void HeapSort(int[] nums) {
// 建堆操作:堆化除叶节点以外的其他所有节点 // 建堆操作:堆化除叶节点以外的其他所有节点
for (int i = nums.Length / 2 - 1; i >= 0; i--) { for (int i = nums.Length / 2 - 1; i >= 0; i--) {
SiftDown(nums, nums.Length, i); SiftDown(nums, nums.Length, i);
@@ -45,7 +45,7 @@ public class heap_sort {
[Test] [Test]
public void Test() { public void Test() {
int[] nums = { 4, 1, 3, 1, 5, 2 }; int[] nums = [4, 1, 3, 1, 5, 2];
HeapSort(nums); HeapSort(nums);
Console.WriteLine("堆排序完成后 nums = " + string.Join(" ", nums)); Console.WriteLine("堆排序完成后 nums = " + string.Join(" ", nums));
} }
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_sorting;
public class insertion_sort { public class insertion_sort {
/* 插入排序 */ /* 插入排序 */
static void InsertionSort(int[] nums) { void InsertionSort(int[] nums) {
// 外循环:已排序元素数量为 1, 2, ..., n // 外循环:已排序元素数量为 1, 2, ..., n
for (int i = 1; i < nums.Length; i++) { for (int i = 1; i < nums.Length; i++) {
int bas = nums[i], j = i - 1; int bas = nums[i], j = i - 1;
@@ -23,7 +23,7 @@ public class insertion_sort {
[Test] [Test]
public void Test() { public void Test() {
int[] nums = { 4, 1, 3, 1, 5, 2 }; int[] nums = [4, 1, 3, 1, 5, 2];
InsertionSort(nums); InsertionSort(nums);
Console.WriteLine("插入排序完成后 nums = " + string.Join(",", nums)); Console.WriteLine("插入排序完成后 nums = " + string.Join(",", nums));
} }
+3 -3
View File
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_sorting;
public class merge_sort { public class merge_sort {
/* 合并左子数组和右子数组 */ /* 合并左子数组和右子数组 */
static void Merge(int[] nums, int left, int mid, int right) { void Merge(int[] nums, int left, int mid, int right) {
// 左子数组区间 [left, mid], 右子数组区间 [mid+1, right] // 左子数组区间 [left, mid], 右子数组区间 [mid+1, right]
// 创建一个临时数组 tmp ,用于存放合并后的结果 // 创建一个临时数组 tmp ,用于存放合并后的结果
int[] tmp = new int[right - left + 1]; int[] tmp = new int[right - left + 1];
@@ -35,7 +35,7 @@ public class merge_sort {
} }
/* 归并排序 */ /* 归并排序 */
static void MergeSort(int[] nums, int left, int right) { void MergeSort(int[] nums, int left, int right) {
// 终止条件 // 终止条件
if (left >= right) return; // 当子数组长度为 1 时终止递归 if (left >= right) return; // 当子数组长度为 1 时终止递归
// 划分阶段 // 划分阶段
@@ -49,7 +49,7 @@ public class merge_sort {
[Test] [Test]
public void Test() { public void Test() {
/* 归并排序 */ /* 归并排序 */
int[] nums = { 7, 3, 2, 6, 0, 1, 5, 4 }; int[] nums = [7, 3, 2, 6, 0, 1, 5, 4];
MergeSort(nums, 0, nums.Length - 1); MergeSort(nums, 0, nums.Length - 1);
Console.WriteLine("归并排序完成后 nums = " + string.Join(",", nums)); Console.WriteLine("归并排序完成后 nums = " + string.Join(",", nums));
} }
+3 -3
View File
@@ -135,17 +135,17 @@ public class quick_sort {
[Test] [Test]
public void Test() { public void Test() {
/* 快速排序 */ /* 快速排序 */
int[] nums = { 2, 4, 1, 0, 3, 5 }; int[] nums = [2, 4, 1, 0, 3, 5];
quickSort.QuickSort(nums, 0, nums.Length - 1); quickSort.QuickSort(nums, 0, nums.Length - 1);
Console.WriteLine("快速排序完成后 nums = " + string.Join(",", nums)); Console.WriteLine("快速排序完成后 nums = " + string.Join(",", nums));
/* 快速排序(中位基准数优化) */ /* 快速排序(中位基准数优化) */
int[] nums1 = { 2, 4, 1, 0, 3, 5 }; int[] nums1 = [2, 4, 1, 0, 3, 5];
QuickSortMedian.QuickSort(nums1, 0, nums1.Length - 1); QuickSortMedian.QuickSort(nums1, 0, nums1.Length - 1);
Console.WriteLine("快速排序(中位基准数优化)完成后 nums1 = " + string.Join(",", nums1)); Console.WriteLine("快速排序(中位基准数优化)完成后 nums1 = " + string.Join(",", nums1));
/* 快速排序(尾递归优化) */ /* 快速排序(尾递归优化) */
int[] nums2 = { 2, 4, 1, 0, 3, 5 }; int[] nums2 = [2, 4, 1, 0, 3, 5];
QuickSortTailCall.QuickSort(nums2, 0, nums2.Length - 1); QuickSortTailCall.QuickSort(nums2, 0, nums2.Length - 1);
Console.WriteLine("快速排序(尾递归优化)完成后 nums2 = " + string.Join(",", nums2)); Console.WriteLine("快速排序(尾递归优化)完成后 nums2 = " + string.Join(",", nums2));
} }
+5 -5
View File
@@ -8,13 +8,13 @@ namespace hello_algo.chapter_sorting;
public class radix_sort { public class radix_sort {
/* 获取元素 num 的第 k 位,其中 exp = 10^(k-1) */ /* 获取元素 num 的第 k 位,其中 exp = 10^(k-1) */
static int Digit(int num, int exp) { int Digit(int num, int exp) {
// 传入 exp 而非 k 可以避免在此重复执行昂贵的次方计算 // 传入 exp 而非 k 可以避免在此重复执行昂贵的次方计算
return (num / exp) % 10; return (num / exp) % 10;
} }
/* 计数排序(根据 nums 第 k 位排序) */ /* 计数排序(根据 nums 第 k 位排序) */
static void CountingSortDigit(int[] nums, int exp) { void CountingSortDigit(int[] nums, int exp) {
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶 // 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
int[] counter = new int[10]; int[] counter = new int[10];
int n = nums.Length; int n = nums.Length;
@@ -42,7 +42,7 @@ public class radix_sort {
} }
/* 基数排序 */ /* 基数排序 */
static void RadixSort(int[] nums) { void RadixSort(int[] nums) {
// 获取数组的最大元素,用于判断最大位数 // 获取数组的最大元素,用于判断最大位数
int m = int.MinValue; int m = int.MinValue;
foreach (int num in nums) { foreach (int num in nums) {
@@ -61,8 +61,8 @@ public class radix_sort {
[Test] [Test]
public void Test() { public void Test() {
// 基数排序 // 基数排序
int[] nums = { 10546151, 35663510, 42865989, 34862445, 81883077, int[] nums = [ 10546151, 35663510, 42865989, 34862445, 81883077,
88906420, 72429244, 30524779, 82060337, 63832996 }; 88906420, 72429244, 30524779, 82060337, 63832996 ];
RadixSort(nums); RadixSort(nums);
Console.WriteLine("基数排序完成后 nums = " + string.Join(" ", nums)); Console.WriteLine("基数排序完成后 nums = " + string.Join(" ", nums));
} }
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_sorting;
public class selection_sort { public class selection_sort {
/* 选择排序 */ /* 选择排序 */
public static void SelectionSort(int[] nums) { void SelectionSort(int[] nums) {
int n = nums.Length; int n = nums.Length;
// 外循环:未排序区间为 [i, n-1] // 外循环:未排序区间为 [i, n-1]
for (int i = 0; i < n - 1; i++) { for (int i = 0; i < n - 1; i++) {
@@ -25,7 +25,7 @@ public class selection_sort {
[Test] [Test]
public void Test() { public void Test() {
int[] nums = { 4, 1, 3, 1, 5, 2 }; int[] nums = [4, 1, 3, 1, 5, 2];
SelectionSort(nums); SelectionSort(nums);
Console.WriteLine("选择排序完成后 nums = " + string.Join(" ", nums)); Console.WriteLine("选择排序完成后 nums = " + string.Join(" ", nums));
} }
@@ -8,18 +8,18 @@ namespace hello_algo.chapter_stack_and_queue;
/* 基于环形数组实现的双向队列 */ /* 基于环形数组实现的双向队列 */
public class ArrayDeque { public class ArrayDeque {
private readonly int[] nums; // 用于存储双向队列元素的数组 int[] nums; // 用于存储双向队列元素的数组
private int front; // 队首指针,指向队首元素 int front; // 队首指针,指向队首元素
private int queSize; // 双向队列长度 int queSize; // 双向队列长度
/* 构造方法 */ /* 构造方法 */
public ArrayDeque(int capacity) { public ArrayDeque(int capacity) {
this.nums = new int[capacity]; nums = new int[capacity];
front = queSize = 0; front = queSize = 0;
} }
/* 获取双向队列的容量 */ /* 获取双向队列的容量 */
public int Capacity() { int Capacity() {
return nums.Length; return nums.Length;
} }
@@ -34,7 +34,7 @@ public class ArrayDeque {
} }
/* 计算环形数组索引 */ /* 计算环形数组索引 */
private int Index(int i) { int Index(int i) {
// 通过取余操作实现数组首尾相连 // 通过取余操作实现数组首尾相连
// 当 i 越过数组尾部后,回到头部 // 当 i 越过数组尾部后,回到头部
// 当 i 越过数组头部后,回到尾部 // 当 i 越过数组头部后,回到尾部
@@ -8,9 +8,9 @@ namespace hello_algo.chapter_stack_and_queue;
/* 基于环形数组实现的队列 */ /* 基于环形数组实现的队列 */
class ArrayQueue { class ArrayQueue {
private readonly int[] nums; // 用于存储队列元素的数组 int[] nums; // 用于存储队列元素的数组
private int front; // 队首指针,指向队首元素 int front; // 队首指针,指向队首元素
private int queSize; // 队列长度 int queSize; // 队列长度
public ArrayQueue(int capacity) { public ArrayQueue(int capacity) {
nums = new int[capacity]; nums = new int[capacity];
@@ -18,7 +18,7 @@ class ArrayQueue {
} }
/* 获取队列的容量 */ /* 获取队列的容量 */
public int Capacity() { int Capacity() {
return nums.Length; return nums.Length;
} }
@@ -8,10 +8,10 @@ namespace hello_algo.chapter_stack_and_queue;
/* 基于数组实现的栈 */ /* 基于数组实现的栈 */
class ArrayStack { class ArrayStack {
private readonly List<int> stack; List<int> stack;
public ArrayStack() { public ArrayStack() {
// 初始化列表(动态数组) // 初始化列表(动态数组)
stack = new(); stack = [];
} }
/* 获取栈的长度 */ /* 获取栈的长度 */
@@ -47,7 +47,7 @@ class ArrayStack {
/* 将 List 转化为 Array 并返回 */ /* 将 List 转化为 Array 并返回 */
public int[] ToArray() { public int[] ToArray() {
return stack.ToArray(); return [.. stack];
} }
} }
@@ -7,22 +7,16 @@
namespace hello_algo.chapter_stack_and_queue; namespace hello_algo.chapter_stack_and_queue;
/* 双向链表节点 */ /* 双向链表节点 */
public class ListNode { public class ListNode(int val) {
public int val; // 节点值 public int val = val; // 节点值
public ListNode? next; // 后继节点引用 public ListNode? next = null; // 后继节点引用
public ListNode? prev; // 前驱节点引用 public ListNode? prev = null; // 前驱节点引用
public ListNode(int val) {
this.val = val;
prev = null;
next = null;
}
} }
/* 基于双向链表实现的双向队列 */ /* 基于双向链表实现的双向队列 */
public class LinkedListDeque { public class LinkedListDeque {
private ListNode? front, rear; // 头节点 front, 尾节点 rear ListNode? front, rear; // 头节点 front, 尾节点 rear
private int queSize = 0; // 双向队列的长度 int queSize = 0; // 双向队列的长度
public LinkedListDeque() { public LinkedListDeque() {
front = null; front = null;
@@ -40,7 +34,7 @@ public class LinkedListDeque {
} }
/* 入队操作 */ /* 入队操作 */
private void Push(int num, bool isFront) { void Push(int num, bool isFront) {
ListNode node = new(num); ListNode node = new(num);
// 若链表为空,则令 front, rear 都指向 node // 若链表为空,则令 front, rear 都指向 node
if (IsEmpty()) { if (IsEmpty()) {
@@ -50,14 +44,14 @@ public class LinkedListDeque {
// 队首入队操作 // 队首入队操作
else if (isFront) { else if (isFront) {
// 将 node 添加至链表头部 // 将 node 添加至链表头部
front.prev = node; front!.prev = node;
node.next = front; node.next = front;
front = node; // 更新头节点 front = node; // 更新头节点
} }
// 队尾入队操作 // 队尾入队操作
else { else {
// 将 node 添加至链表尾部 // 将 node 添加至链表尾部
rear.next = node; rear!.next = node;
node.prev = rear; node.prev = rear;
rear = node; // 更新尾节点 rear = node; // 更新尾节点
} }
@@ -76,7 +70,7 @@ public class LinkedListDeque {
} }
/* 出队操作 */ /* 出队操作 */
private int? Pop(bool isFront) { int? Pop(bool isFront) {
if (IsEmpty()) if (IsEmpty())
throw new Exception(); throw new Exception();
int? val; int? val;
@@ -87,7 +81,7 @@ public class LinkedListDeque {
ListNode? fNext = front?.next; ListNode? fNext = front?.next;
if (fNext != null) { if (fNext != null) {
fNext.prev = null; fNext.prev = null;
front.next = null; front!.next = null;
} }
front = fNext; // 更新头节点 front = fNext; // 更新头节点
} }
@@ -98,7 +92,7 @@ public class LinkedListDeque {
ListNode? rPrev = rear?.prev; ListNode? rPrev = rear?.prev;
if (rPrev != null) { if (rPrev != null) {
rPrev.next = null; rPrev.next = null;
rear.prev = null; rear!.prev = null;
} }
rear = rPrev; // 更新尾节点 rear = rPrev; // 更新尾节点
} }
@@ -8,8 +8,8 @@ namespace hello_algo.chapter_stack_and_queue;
/* 基于链表实现的队列 */ /* 基于链表实现的队列 */
class LinkedListQueue { class LinkedListQueue {
private ListNode? front, rear; // 头节点 front ,尾节点 rear ListNode? front, rear; // 头节点 front ,尾节点 rear
private int queSize = 0; int queSize = 0;
public LinkedListQueue() { public LinkedListQueue() {
front = null; front = null;
@@ -55,18 +55,18 @@ class LinkedListQueue {
public int Peek() { public int Peek() {
if (IsEmpty()) if (IsEmpty())
throw new Exception(); throw new Exception();
return front.val; return front!.val;
} }
/* 将链表转化为 Array 并返回 */ /* 将链表转化为 Array 并返回 */
public int[] ToArray() { public int[] ToArray() {
if (front == null) if (front == null)
return Array.Empty<int>(); return [];
ListNode node = front; ListNode? node = front;
int[] res = new int[Size()]; int[] res = new int[Size()];
for (int i = 0; i < res.Length; i++) { for (int i = 0; i < res.Length; i++) {
res[i] = node.val; res[i] = node!.val;
node = node.next; node = node.next;
} }
return res; return res;
@@ -8,8 +8,8 @@ namespace hello_algo.chapter_stack_and_queue;
/* 基于链表实现的栈 */ /* 基于链表实现的栈 */
class LinkedListStack { class LinkedListStack {
private ListNode? stackPeek; // 将头节点作为栈顶 ListNode? stackPeek; // 将头节点作为栈顶
private int stkSize = 0; // 栈的长度 int stkSize = 0; // 栈的长度
public LinkedListStack() { public LinkedListStack() {
stackPeek = null; stackPeek = null;
@@ -37,7 +37,7 @@ class LinkedListStack {
/* 出栈 */ /* 出栈 */
public int Pop() { public int Pop() {
int num = Peek(); int num = Peek();
stackPeek = stackPeek.next; stackPeek = stackPeek!.next;
stkSize--; stkSize--;
return num; return num;
} }
@@ -46,18 +46,18 @@ class LinkedListStack {
public int Peek() { public int Peek() {
if (IsEmpty()) if (IsEmpty())
throw new Exception(); throw new Exception();
return stackPeek.val; return stackPeek!.val;
} }
/* 将 List 转化为 Array 并返回 */ /* 将 List 转化为 Array 并返回 */
public int[] ToArray() { public int[] ToArray() {
if (stackPeek == null) if (stackPeek == null)
return Array.Empty<int>(); return [];
ListNode node = stackPeek; ListNode? node = stackPeek;
int[] res = new int[Size()]; int[] res = new int[Size()];
for (int i = res.Length - 1; i >= 0; i--) { for (int i = res.Length - 1; i >= 0; i--) {
res[i] = node.val; res[i] = node!.val;
node = node.next; node = node.next;
} }
return res; return res;
+13 -18
View File
@@ -7,13 +7,8 @@
namespace hello_algo.chapter_tree; namespace hello_algo.chapter_tree;
/* 数组表示下的二叉树类 */ /* 数组表示下的二叉树类 */
public class ArrayBinaryTree { public class ArrayBinaryTree(List<int?> arr) {
private readonly List<int?> tree; List<int?> tree = new(arr);
/* 构造方法 */
public ArrayBinaryTree(List<int?> arr) {
tree = new List<int?>(arr);
}
/* 节点数量 */ /* 节点数量 */
public int Size() { public int Size() {
@@ -45,50 +40,50 @@ public class ArrayBinaryTree {
/* 层序遍历 */ /* 层序遍历 */
public List<int> LevelOrder() { public List<int> LevelOrder() {
List<int> res = new(); List<int> res = [];
// 直接遍历数组 // 直接遍历数组
for (int i = 0; i < Size(); i++) { for (int i = 0; i < Size(); i++) {
if (Val(i).HasValue) if (Val(i).HasValue)
res.Add(Val(i).Value); res.Add(Val(i)!.Value);
} }
return res; return res;
} }
/* 深度优先遍历 */ /* 深度优先遍历 */
private void DFS(int i, string order, List<int> res) { void DFS(int i, string order, List<int> res) {
// 若为空位,则返回 // 若为空位,则返回
if (!Val(i).HasValue) if (!Val(i).HasValue)
return; return;
// 前序遍历 // 前序遍历
if (order == "pre") if (order == "pre")
res.Add(Val(i).Value); res.Add(Val(i)!.Value);
DFS(Left(i), order, res); DFS(Left(i), order, res);
// 中序遍历 // 中序遍历
if (order == "in") if (order == "in")
res.Add(Val(i).Value); res.Add(Val(i)!.Value);
DFS(Right(i), order, res); DFS(Right(i), order, res);
// 后序遍历 // 后序遍历
if (order == "post") if (order == "post")
res.Add(Val(i).Value); res.Add(Val(i)!.Value);
} }
/* 前序遍历 */ /* 前序遍历 */
public List<int> PreOrder() { public List<int> PreOrder() {
List<int> res = new(); List<int> res = [];
DFS(0, "pre", res); DFS(0, "pre", res);
return res; return res;
} }
/* 中序遍历 */ /* 中序遍历 */
public List<int> InOrder() { public List<int> InOrder() {
List<int> res = new(); List<int> res = [];
DFS(0, "in", res); DFS(0, "in", res);
return res; return res;
} }
/* 后序遍历 */ /* 后序遍历 */
public List<int> PostOrder() { public List<int> PostOrder() {
List<int> res = new(); List<int> res = [];
DFS(0, "post", res); DFS(0, "post", res);
return res; return res;
} }
@@ -99,9 +94,9 @@ public class array_binary_tree {
public void Test() { public void Test() {
// 初始化二叉树 // 初始化二叉树
// 这里借助了一个从数组直接生成二叉树的函数 // 这里借助了一个从数组直接生成二叉树的函数
List<int?> arr = new() { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 }; List<int?> arr = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
TreeNode root = TreeNode.ListToTree(arr); TreeNode? root = TreeNode.ListToTree(arr);
Console.WriteLine("\n初始化二叉树\n"); Console.WriteLine("\n初始化二叉树\n");
Console.WriteLine("二叉树的数组表示:"); Console.WriteLine("二叉树的数组表示:");
Console.WriteLine(arr.PrintList()); Console.WriteLine(arr.PrintList());
+11 -11
View File
@@ -11,13 +11,13 @@ class AVLTree {
public TreeNode? root; // 根节点 public TreeNode? root; // 根节点
/* 获取节点高度 */ /* 获取节点高度 */
public int Height(TreeNode? node) { int Height(TreeNode? node) {
// 空节点高度为 -1 ,叶节点高度为 0 // 空节点高度为 -1 ,叶节点高度为 0
return node == null ? -1 : node.height; return node == null ? -1 : node.height;
} }
/* 更新节点高度 */ /* 更新节点高度 */
private void UpdateHeight(TreeNode node) { void UpdateHeight(TreeNode node) {
// 节点高度等于最高子树高度 + 1 // 节点高度等于最高子树高度 + 1
node.height = Math.Max(Height(node.left), Height(node.right)) + 1; node.height = Math.Max(Height(node.left), Height(node.right)) + 1;
} }
@@ -32,7 +32,7 @@ class AVLTree {
/* 右旋操作 */ /* 右旋操作 */
TreeNode? RightRotate(TreeNode? node) { TreeNode? RightRotate(TreeNode? node) {
TreeNode? child = node.left; TreeNode? child = node?.left;
TreeNode? grandChild = child?.right; TreeNode? grandChild = child?.right;
// 以 child 为原点,将 node 向右旋转 // 以 child 为原点,将 node 向右旋转
child.right = node; child.right = node;
@@ -46,7 +46,7 @@ class AVLTree {
/* 左旋操作 */ /* 左旋操作 */
TreeNode? LeftRotate(TreeNode? node) { TreeNode? LeftRotate(TreeNode? node) {
TreeNode? child = node.right; TreeNode? child = node?.right;
TreeNode? grandChild = child?.left; TreeNode? grandChild = child?.left;
// 以 child 为原点,将 node 向左旋转 // 以 child 为原点,将 node 向左旋转
child.left = node; child.left = node;
@@ -64,23 +64,23 @@ class AVLTree {
int balanceFactorInt = BalanceFactor(node); int balanceFactorInt = BalanceFactor(node);
// 左偏树 // 左偏树
if (balanceFactorInt > 1) { if (balanceFactorInt > 1) {
if (BalanceFactor(node.left) >= 0) { if (BalanceFactor(node?.left) >= 0) {
// 右旋 // 右旋
return RightRotate(node); return RightRotate(node);
} else { } else {
// 先左旋后右旋 // 先左旋后右旋
node.left = LeftRotate(node?.left); node!.left = LeftRotate(node!.left);
return RightRotate(node); return RightRotate(node);
} }
} }
// 右偏树 // 右偏树
if (balanceFactorInt < -1) { if (balanceFactorInt < -1) {
if (BalanceFactor(node.right) <= 0) { if (BalanceFactor(node?.right) <= 0) {
// 左旋 // 左旋
return LeftRotate(node); return LeftRotate(node);
} else { } else {
// 先右旋后左旋 // 先右旋后左旋
node.right = RightRotate(node?.right); node!.right = RightRotate(node!.right);
return LeftRotate(node); return LeftRotate(node);
} }
} }
@@ -94,7 +94,7 @@ class AVLTree {
} }
/* 递归插入节点(辅助方法) */ /* 递归插入节点(辅助方法) */
private TreeNode? InsertHelper(TreeNode? node, int val) { TreeNode? InsertHelper(TreeNode? node, int val) {
if (node == null) return new TreeNode(val); if (node == null) return new TreeNode(val);
/* 1. 查找插入位置,并插入节点 */ /* 1. 查找插入位置,并插入节点 */
if (val < node.val) if (val < node.val)
@@ -116,7 +116,7 @@ class AVLTree {
} }
/* 递归删除节点(辅助方法) */ /* 递归删除节点(辅助方法) */
private TreeNode? RemoveHelper(TreeNode? node, int val) { TreeNode? RemoveHelper(TreeNode? node, int val) {
if (node == null) return null; if (node == null) return null;
/* 1. 查找节点,并删除之 */ /* 1. 查找节点,并删除之 */
if (val < node.val) if (val < node.val)
@@ -138,7 +138,7 @@ class AVLTree {
while (temp.left != null) { while (temp.left != null) {
temp = temp.left; temp = temp.left;
} }
node.right = RemoveHelper(node.right, temp.val); node.right = RemoveHelper(node.right, temp.val!.Value);
node.val = temp.val; node.val = temp.val;
} }
} }
@@ -99,7 +99,7 @@ class BinarySearchTree {
TreeNode? child = cur.left ?? cur.right; TreeNode? child = cur.left ?? cur.right;
// 删除节点 cur // 删除节点 cur
if (cur != root) { if (cur != root) {
if (pre.left == cur) if (pre!.left == cur)
pre.left = child; pre.left = child;
else else
pre.right = child; pre.right = child;
@@ -116,7 +116,7 @@ class BinarySearchTree {
tmp = tmp.left; tmp = tmp.left;
} }
// 递归删除节点 tmp // 递归删除节点 tmp
Remove(tmp.val); Remove(tmp.val!.Value);
// 用 tmp 覆盖 cur // 用 tmp 覆盖 cur
cur.val = tmp.val; cur.val = tmp.val;
} }
@@ -129,7 +129,7 @@ public class binary_search_tree {
/* 初始化二叉搜索树 */ /* 初始化二叉搜索树 */
BinarySearchTree bst = new(); BinarySearchTree bst = new();
// 请注意,不同的插入顺序会生成不同的二叉树,该序列可以生成一个完美二叉树 // 请注意,不同的插入顺序会生成不同的二叉树,该序列可以生成一个完美二叉树
int[] nums = { 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 }; int[] nums = [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15];
foreach (int num in nums) { foreach (int num in nums) {
bst.Insert(num); bst.Insert(num);
} }
@@ -139,7 +139,7 @@ public class binary_search_tree {
/* 查找节点 */ /* 查找节点 */
TreeNode? node = bst.Search(7); TreeNode? node = bst.Search(7);
Console.WriteLine("\n查找到的节点对象为 " + node + ",节点值 = " + node.val); Console.WriteLine("\n查找到的节点对象为 " + node + ",节点值 = " + node?.val);
/* 插入节点 */ /* 插入节点 */
bst.Insert(16); bst.Insert(16);
+5 -5
View File
@@ -9,15 +9,15 @@ namespace hello_algo.chapter_tree;
public class binary_tree_bfs { public class binary_tree_bfs {
/* 层序遍历 */ /* 层序遍历 */
public List<int> LevelOrder(TreeNode root) { List<int> LevelOrder(TreeNode root) {
// 初始化队列,加入根节点 // 初始化队列,加入根节点
Queue<TreeNode> queue = new(); Queue<TreeNode> queue = new();
queue.Enqueue(root); queue.Enqueue(root);
// 初始化一个列表,用于保存遍历序列 // 初始化一个列表,用于保存遍历序列
List<int> list = new(); List<int> list = [];
while (queue.Count != 0) { while (queue.Count != 0) {
TreeNode node = queue.Dequeue(); // 队列出队 TreeNode node = queue.Dequeue(); // 队列出队
list.Add(node.val); // 保存节点值 list.Add(node.val!.Value); // 保存节点值
if (node.left != null) if (node.left != null)
queue.Enqueue(node.left); // 左子节点入队 queue.Enqueue(node.left); // 左子节点入队
if (node.right != null) if (node.right != null)
@@ -30,11 +30,11 @@ public class binary_tree_bfs {
public void Test() { public void Test() {
/* 初始化二叉树 */ /* 初始化二叉树 */
// 这里借助了一个从数组直接生成二叉树的函数 // 这里借助了一个从数组直接生成二叉树的函数
TreeNode? root = TreeNode.ListToTree(new List<int?> { 1, 2, 3, 4, 5, 6, 7 }); TreeNode? root = TreeNode.ListToTree([1, 2, 3, 4, 5, 6, 7]);
Console.WriteLine("\n初始化二叉树\n"); Console.WriteLine("\n初始化二叉树\n");
PrintUtil.PrintTree(root); PrintUtil.PrintTree(root);
List<int> list = LevelOrder(root); List<int> list = LevelOrder(root!);
Console.WriteLine("\n层序遍历的节点打印序列 = " + string.Join(",", list)); Console.WriteLine("\n层序遍历的节点打印序列 = " + string.Join(",", list));
} }
} }
+5 -5
View File
@@ -7,13 +7,13 @@
namespace hello_algo.chapter_tree; namespace hello_algo.chapter_tree;
public class binary_tree_dfs { public class binary_tree_dfs {
readonly List<int> list = new(); List<int> list = [];
/* 前序遍历 */ /* 前序遍历 */
void PreOrder(TreeNode? root) { void PreOrder(TreeNode? root) {
if (root == null) return; if (root == null) return;
// 访问优先级:根节点 -> 左子树 -> 右子树 // 访问优先级:根节点 -> 左子树 -> 右子树
list.Add(root.val); list.Add(root.val!.Value);
PreOrder(root.left); PreOrder(root.left);
PreOrder(root.right); PreOrder(root.right);
} }
@@ -23,7 +23,7 @@ public class binary_tree_dfs {
if (root == null) return; if (root == null) return;
// 访问优先级:左子树 -> 根节点 -> 右子树 // 访问优先级:左子树 -> 根节点 -> 右子树
InOrder(root.left); InOrder(root.left);
list.Add(root.val); list.Add(root.val!.Value);
InOrder(root.right); InOrder(root.right);
} }
@@ -33,14 +33,14 @@ public class binary_tree_dfs {
// 访问优先级:左子树 -> 右子树 -> 根节点 // 访问优先级:左子树 -> 右子树 -> 根节点
PostOrder(root.left); PostOrder(root.left);
PostOrder(root.right); PostOrder(root.right);
list.Add(root.val); list.Add(root.val!.Value);
} }
[Test] [Test]
public void Test() { public void Test() {
/* 初始化二叉树 */ /* 初始化二叉树 */
// 这里借助了一个从数组直接生成二叉树的函数 // 这里借助了一个从数组直接生成二叉树的函数
TreeNode? root = TreeNode.ListToTree(new List<int?> { 1, 2, 3, 4, 5, 6, 7 }); TreeNode? root = TreeNode.ListToTree([1, 2, 3, 4, 5, 6, 7]);
Console.WriteLine("\n初始化二叉树\n"); Console.WriteLine("\n初始化二叉树\n");
PrintUtil.PrintTree(root); PrintUtil.PrintTree(root);
+8 -5
View File
@@ -2,17 +2,20 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<RootNamespace>hello_algo</RootNamespace> <RootNamespace>hello_algo</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.13.3" /> <PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" /> <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" /> <PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup> </ItemGroup>
</Project> </Project>
+3 -7
View File
@@ -5,14 +5,10 @@
namespace hello_algo.utils; namespace hello_algo.utils;
/* Definition for a singly-linked list node */ /* Definition for a singly-linked list node */
public class ListNode { public class ListNode(int x) {
public int val; public int val = x;
public ListNode? next; public ListNode? next;
public ListNode(int x) {
val = x;
}
/* Generate a linked list with an array */ /* Generate a linked list with an array */
public static ListNode? ArrToLinkedList(int[] arr) { public static ListNode? ArrToLinkedList(int[] arr) {
ListNode dum = new(0); ListNode dum = new(0);
@@ -33,7 +29,7 @@ public class ListNode {
} }
public override string? ToString() { public override string? ToString() {
List<string> list = new(); List<string> list = [];
var head = this; var head = this;
while (head != null) { while (head != null) {
list.Add(head.val.ToString()); list.Add(head.val.ToString());
+9 -14
View File
@@ -6,14 +6,9 @@
namespace hello_algo.utils; namespace hello_algo.utils;
public class Trunk { public class Trunk(Trunk? prev, string str) {
public Trunk? prev; public Trunk? prev = prev;
public string str; public string str = str;
public Trunk(Trunk? prev, string str) {
this.prev = prev;
this.str = str;
}
}; };
public static class PrintUtil { public static class PrintUtil {
@@ -45,8 +40,8 @@ public static class PrintUtil {
} }
/* Print a linked list */ /* Print a linked list */
public static void PrintLinkedList(ListNode head) { public static void PrintLinkedList(ListNode? head) {
List<string> list = new(); List<string> list = [];
while (head != null) { while (head != null) {
list.Add(head.val.ToString()); list.Add(head.val.ToString());
head = head.next; head = head.next;
@@ -115,10 +110,10 @@ public static class PrintUtil {
/* Print a heap */ /* Print a heap */
public static void PrintHeap(Queue<int> queue) { public static void PrintHeap(Queue<int> queue) {
Console.Write("堆的数组表示:"); Console.Write("堆的数组表示:");
List<int> list = queue.ToList(); List<int> list = [.. queue];
Console.WriteLine(string.Join(',', list)); Console.WriteLine(string.Join(',', list));
Console.WriteLine("堆的树状表示:"); Console.WriteLine("堆的树状表示:");
TreeNode tree = TreeNode.ListToTree(list.Cast<int?>().ToList()); TreeNode? tree = TreeNode.ListToTree(list.Cast<int?>().ToList());
PrintTree(tree); PrintTree(tree);
} }
@@ -126,13 +121,13 @@ public static class PrintUtil {
public static void PrintHeap(PriorityQueue<int, int> queue) { public static void PrintHeap(PriorityQueue<int, int> queue) {
var newQueue = new PriorityQueue<int, int>(queue.UnorderedItems, queue.Comparer); var newQueue = new PriorityQueue<int, int>(queue.UnorderedItems, queue.Comparer);
Console.Write("堆的数组表示:"); Console.Write("堆的数组表示:");
List<int> list = new(); List<int> list = [];
while (newQueue.TryDequeue(out int element, out _)) { while (newQueue.TryDequeue(out int element, out _)) {
list.Add(element); list.Add(element);
} }
Console.WriteLine("堆的树状表示:"); Console.WriteLine("堆的树状表示:");
Console.WriteLine(string.Join(',', list.ToList())); Console.WriteLine(string.Join(',', list.ToList()));
TreeNode tree = TreeNode.ListToTree(list.Cast<int?>().ToList()); TreeNode? tree = TreeNode.ListToTree(list.Cast<int?>().ToList());
PrintTree(tree); PrintTree(tree);
} }
} }
+7 -12
View File
@@ -7,17 +7,12 @@
namespace hello_algo.utils; namespace hello_algo.utils;
/* 二叉树节点类 */ /* 二叉树节点类 */
public class TreeNode { public class TreeNode(int? x) {
public int val; // 节点值 public int? val = x; // 节点值
public int height; // 节点高度 public int height; // 节点高度
public TreeNode? left; // 左子节点引用 public TreeNode? left; // 左子节点引用
public TreeNode? right; // 右子节点引用 public TreeNode? right; // 右子节点引用
/* 构造方法 */
public TreeNode(int x) {
val = x;
}
// 序列化编码规则请参考: // 序列化编码规则请参考:
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/ // https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
// 二叉树的数组表示: // 二叉树的数组表示:
@@ -35,11 +30,11 @@ public class TreeNode {
// \——— 8 // \——— 8
/* 将列表反序列化为二叉树:递归 */ /* 将列表反序列化为二叉树:递归 */
private static TreeNode? ListToTreeDFS(List<int?> arr, int i) { static TreeNode? ListToTreeDFS(List<int?> arr, int i) {
if (i < 0 || i >= arr.Count || !arr[i].HasValue) { if (i < 0 || i >= arr.Count || !arr[i].HasValue) {
return null; return null;
} }
TreeNode root = new(arr[i].Value) { TreeNode root = new(arr[i]) {
left = ListToTreeDFS(arr, 2 * i + 1), left = ListToTreeDFS(arr, 2 * i + 1),
right = ListToTreeDFS(arr, 2 * i + 2) right = ListToTreeDFS(arr, 2 * i + 2)
}; };
@@ -52,7 +47,7 @@ public class TreeNode {
} }
/* 将二叉树序列化为列表:递归 */ /* 将二叉树序列化为列表:递归 */
private static void TreeToListDFS(TreeNode? root, int i, List<int?> res) { static void TreeToListDFS(TreeNode? root, int i, List<int?> res) {
if (root == null) if (root == null)
return; return;
while (i >= res.Count) { while (i >= res.Count) {
@@ -65,7 +60,7 @@ public class TreeNode {
/* 将二叉树序列化为列表 */ /* 将二叉树序列化为列表 */
public static List<int?> TreeToList(TreeNode root) { public static List<int?> TreeToList(TreeNode root) {
List<int?> res = new(); List<int?> res = [];
TreeToListDFS(root, 0, res); TreeToListDFS(root, 0, res);
return res; return res;
} }
+3 -6
View File
@@ -7,11 +7,8 @@
namespace hello_algo.utils; namespace hello_algo.utils;
/* 顶点类 */ /* 顶点类 */
public class Vertex { public class Vertex(int val) {
public int val; public int val = val;
public Vertex(int val) {
this.val = val;
}
/* 输入值列表 vals ,返回顶点列表 vets */ /* 输入值列表 vals ,返回顶点列表 vets */
public static Vertex[] ValsToVets(int[] vals) { public static Vertex[] ValsToVets(int[] vals) {
@@ -24,7 +21,7 @@ public class Vertex {
/* 输入顶点列表 vets ,返回值列表 vals */ /* 输入顶点列表 vets ,返回值列表 vals */
public static List<int> VetsToVals(List<Vertex> vets) { public static List<int> VetsToVals(List<Vertex> vets) {
List<int> vals = new(); List<int> vals = [];
foreach (Vertex vet in vets) { foreach (Vertex vet in vets) {
vals.Add(vet.val); vals.Add(vet.val);
} }
+1 -1
View File
@@ -35,7 +35,7 @@
### C# 环境 ### C# 环境
1. 下载并安装 [.Net 6.0](https://dotnet.microsoft.com/en-us/download) 。 1. 下载并安装 [.Net 8.0](https://dotnet.microsoft.com/en-us/download) 。
2. 在 VSCode 的插件市场中搜索 `C# Dev Kit` ,安装 C# Dev Kit [配置教程](https://code.visualstudio.com/docs/csharp/get-started))。 2. 在 VSCode 的插件市场中搜索 `C# Dev Kit` ,安装 C# Dev Kit [配置教程](https://code.visualstudio.com/docs/csharp/get-started))。
3. 也可使用 Visual Studio[安装教程](https://learn.microsoft.com/zh-cn/visualstudio/install/install-visual-studio?view=vs-2022))。 3. 也可使用 Visual Studio[安装教程](https://learn.microsoft.com/zh-cn/visualstudio/install/install-visual-studio?view=vs-2022))。
+1 -1
View File
@@ -43,7 +43,7 @@
```csharp title="array.cs" ```csharp title="array.cs"
/* 初始化数组 */ /* 初始化数组 */
int[] arr = new int[5]; // { 0, 0, 0, 0, 0 } int[] arr = new int[5]; // { 0, 0, 0, 0, 0 }
int[] nums = { 1, 3, 2, 5, 4 }; int[] nums = [1, 3, 2, 5, 4];
``` ```
=== "Go" === "Go"
@@ -52,10 +52,9 @@
```csharp title="" ```csharp title=""
/* 链表节点类 */ /* 链表节点类 */
class ListNode { class ListNode(int x) { //构造函数
int val; // 节点值 int val = x; // 节点值
ListNode next; // 指向下一节点的引用 ListNode? next; // 指向下一节点的引用
ListNode(int x) => val = x; //构造函数
} }
``` ```
@@ -495,11 +494,10 @@
```csharp title="" ```csharp title=""
/* 双向链表节点类 */ /* 双向链表节点类 */
class ListNode { class ListNode(int x) { // 构造函数
int val; // 节点值 int val = x; // 节点值
ListNode next; // 指向后继节点的引用 ListNode next; // 指向后继节点的引用
ListNode prev; // 指向前驱节点的引用 ListNode prev; // 指向前驱节点的引用
ListNode(int x) => val = x; // 构造函数
} }
``` ```
+4 -4
View File
@@ -54,10 +54,10 @@
```csharp title="list.cs" ```csharp title="list.cs"
/* 初始化列表 */ /* 初始化列表 */
// 无初始值 // 无初始值
List<int> nums1 = new(); List<int> nums1 = [];
// 有初始值 // 有初始值
int[] numbers = new int[] { 1, 3, 2, 5, 4 }; int[] numbers = [1, 3, 2, 5, 4];
List<int> nums = numbers.ToList(); List<int> nums = [.. numbers];
``` ```
=== "Go" === "Go"
@@ -700,7 +700,7 @@
```csharp title="list.cs" ```csharp title="list.cs"
/* 拼接两个列表 */ /* 拼接两个列表 */
List<int> nums1 = new() { 6, 8, 7, 10, 9 }; List<int> nums1 = [6, 8, 7, 10, 9];
nums.AddRange(nums1); // 将列表 nums1 拼接到 nums 之后 nums.AddRange(nums1); // 将列表 nums1 拼接到 nums 之后
``` ```
@@ -98,10 +98,9 @@
```csharp title="" ```csharp title=""
/* 类 */ /* 类 */
class Node { class Node(int x) {
int val; int val = x;
Node next; Node next;
Node(int x) { val = x; }
} }
/* 函数 */ /* 函数 */
@@ -113,7 +112,7 @@
int Algorithm(int n) { // 输入数据 int Algorithm(int n) { // 输入数据
const int a = 0; // 暂存数据(常量) const int a = 0; // 暂存数据(常量)
int b = 0; // 暂存数据(变量) int b = 0; // 暂存数据(变量)
Node node = new(0); // 暂存数据(对象) Node node = new(0); // 暂存数据(对象)
int c = Function(); // 栈帧空间(调用函数) int c = Function(); // 栈帧空间(调用函数)
return a + b + c; // 输出数据 return a + b + c; // 输出数据
} }
+1 -1
View File
@@ -211,7 +211,7 @@ $$
int hashStr = str.GetHashCode(); int hashStr = str.GetHashCode();
// 字符串 Hello 算法 的哈希值为 -586107568; // 字符串 Hello 算法 的哈希值为 -586107568;
object[] arr = { 12836, "小哈" }; object[] arr = [12836, "小哈"];
int hashTup = arr.GetHashCode(); int hashTup = arr.GetHashCode();
// 数组 [12836, 小哈] 的哈希值为 42931033; // 数组 [12836, 小哈] 的哈希值为 42931033;
+1 -1
View File
@@ -187,7 +187,7 @@
bool isEmpty = maxHeap.Count == 0; bool isEmpty = maxHeap.Count == 0;
/* 输入列表并建堆 */ /* 输入列表并建堆 */
minHeap = new PriorityQueue<int, int>(new List<(int, int)> { (1, 1), (3, 3), (2, 2), (5, 5), (4, 4), }); minHeap = new PriorityQueue<int, int>([(1, 1), (3, 3), (2, 2), (5, 5), (4, 4)]);
``` ```
=== "Go" === "Go"
@@ -53,7 +53,7 @@
```csharp title="" ```csharp title=""
/* 二叉树的数组表示 */ /* 二叉树的数组表示 */
// 使用 int? 可空类型 ,就可以使用 null 来标记空位 // 使用 int? 可空类型 ,就可以使用 null 来标记空位
int?[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 }; int?[] tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
``` ```
=== "Go" === "Go"
+5 -6
View File
@@ -63,12 +63,11 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
```csharp title="" ```csharp title=""
/* AVL 树节点类 */ /* AVL 树节点类 */
class TreeNode { class TreeNode(int? x) {
public int val; // 节点值 public int? val = x; // 节点值
public int height; // 节点高度 public int height; // 节点高度
public TreeNode? left; // 左子节点 public TreeNode? left; // 左子节点引用
public TreeNode? right; // 右子节点 public TreeNode? right; // 右子节点引用
public TreeNode(int x) { val = x; }
} }
``` ```
+4 -5
View File
@@ -41,11 +41,10 @@
```csharp title="" ```csharp title=""
/* 二叉树节点类 */ /* 二叉树节点类 */
class TreeNode { class TreeNode(int? x) {
int val; // 节点值 public int? val = x; // 节点值
TreeNode? left; // 左子节点引用 public TreeNode? left; // 左子节点引用
TreeNode? right; // 右子节点引用 public TreeNode? right; // 右子节点引用
TreeNode(int x) { val = x; }
} }
``` ```