fix(csharp): Modify method name to PascalCase, simplify new expression (#840)

* Modify method name to PascalCase(array and linked list)

* Modify method name to PascalCase(backtracking)

* Modify method name to PascalCase(computational complexity)

* Modify method name to PascalCase(divide and conquer)

* Modify method name to PascalCase(dynamic programming)

* Modify method name to PascalCase(graph)

* Modify method name to PascalCase(greedy)

* Modify method name to PascalCase(hashing)

* Modify method name to PascalCase(heap)

* Modify method name to PascalCase(searching)

* Modify method name to PascalCase(sorting)

* Modify method name to PascalCase(stack and queue)

* Modify method name to PascalCase(tree)

* local check
This commit is contained in:
hpstory
2023-10-08 01:33:46 +08:00
committed by GitHub
parent 6f7e768cb7
commit f62256bee1
129 changed files with 1186 additions and 1192 deletions

View File

@@ -6,7 +6,7 @@ namespace hello_algo.chapter_array_and_linkedlist;
public class array {
/* 随机访问元素 */
public static int randomAccess(int[] nums) {
public static int RandomAccess(int[] nums) {
Random random = new();
// 在区间 [0, nums.Length) 中随机抽取一个数字
int randomIndex = random.Next(nums.Length);
@@ -16,7 +16,7 @@ public class array {
}
/* 扩展数组长度 */
public static int[] extend(int[] nums, int enlarge) {
public static int[] Extend(int[] nums, int enlarge) {
// 初始化一个扩展长度后的数组
int[] res = new int[nums.Length + enlarge];
// 将原数组中的所有元素复制到新数组
@@ -28,7 +28,7 @@ public class array {
}
/* 在数组的索引 index 处插入元素 num */
public static void insert(int[] nums, int num, int index) {
public static void Insert(int[] nums, int num, int index) {
// 把索引 index 以及之后的所有元素向后移动一位
for (int i = nums.Length - 1; i > index; i--) {
nums[i] = nums[i - 1];
@@ -38,7 +38,7 @@ public class array {
}
/* 删除索引 index 处元素 */
public static void remove(int[] nums, int index) {
public static void Remove(int[] nums, int index) {
// 把索引 index 之后的所有元素向前移动一位
for (int i = index; i < nums.Length - 1; i++) {
nums[i] = nums[i + 1];
@@ -46,7 +46,7 @@ public class array {
}
/* 遍历数组 */
public static void traverse(int[] nums) {
public static void Traverse(int[] nums) {
int count = 0;
// 通过索引遍历数组
for (int i = 0; i < nums.Length; i++) {
@@ -59,7 +59,7 @@ public class array {
}
/* 在数组中查找指定元素 */
public static int find(int[] nums, int target) {
public static int Find(int[] nums, int target) {
for (int i = 0; i < nums.Length; i++) {
if (nums[i] == target)
return i;
@@ -68,7 +68,7 @@ public class array {
}
/* 辅助函数,数组转字符串 */
public static string toString(int[] nums) {
public static string ToString(int[] nums) {
return string.Join(",", nums);
}
@@ -77,31 +77,31 @@ public class array {
public static void Test() {
// 初始化数组
int[] arr = new int[5];
Console.WriteLine("数组 arr = " + toString(arr));
Console.WriteLine("数组 arr = " + ToString(arr));
int[] nums = { 1, 3, 2, 5, 4 };
Console.WriteLine("数组 nums = " + toString(nums));
Console.WriteLine("数组 nums = " + ToString(nums));
// 随机访问
int randomNum = randomAccess(nums);
int randomNum = RandomAccess(nums);
Console.WriteLine("在 nums 中获取随机元素 " + randomNum);
// 长度扩展
nums = extend(nums, 3);
Console.WriteLine("将数组长度扩展至 8 ,得到 nums = " + toString(nums));
nums = Extend(nums, 3);
Console.WriteLine("将数组长度扩展至 8 ,得到 nums = " + ToString(nums));
// 插入元素
insert(nums, 6, 3);
Console.WriteLine("在索引 3 处插入数字 6 ,得到 nums = " + toString(nums));
Insert(nums, 6, 3);
Console.WriteLine("在索引 3 处插入数字 6 ,得到 nums = " + ToString(nums));
// 删除元素
remove(nums, 2);
Console.WriteLine("删除索引 2 处的元素,得到 nums = " + toString(nums));
Remove(nums, 2);
Console.WriteLine("删除索引 2 处的元素,得到 nums = " + ToString(nums));
// 遍历数组
traverse(nums);
Traverse(nums);
// 查找元素
int index = find(nums, 3);
int index = Find(nums, 3);
Console.WriteLine("在 nums 中查找元素 3 ,得到索引 = " + index);
}
}

View File

@@ -6,14 +6,14 @@ namespace hello_algo.chapter_array_and_linkedlist;
public class linked_list {
/* 在链表的节点 n0 之后插入节点 P */
public static void insert(ListNode n0, ListNode P) {
public static void Insert(ListNode n0, ListNode P) {
ListNode? n1 = n0.next;
P.next = n1;
n0.next = P;
}
/* 删除链表的节点 n0 之后的首个节点 */
public static void remove(ListNode n0) {
public static void Remove(ListNode n0) {
if (n0.next == null)
return;
// n0 -> P -> n1
@@ -23,7 +23,7 @@ public class linked_list {
}
/* 访问链表中索引为 index 的节点 */
public static ListNode? access(ListNode head, int index) {
public static ListNode? Access(ListNode head, int index) {
for (int i = 0; i < index; i++) {
if (head == null)
return null;
@@ -33,7 +33,7 @@ public class linked_list {
}
/* 在链表中查找值为 target 的首个节点 */
public static int find(ListNode head, int target) {
public static int Find(ListNode head, int target) {
int index = 0;
while (head != null) {
if (head.val == target)
@@ -49,11 +49,11 @@ public class linked_list {
public void Test() {
// 初始化链表
// 初始化各个节点
ListNode n0 = new ListNode(1);
ListNode n1 = new ListNode(3);
ListNode n2 = new ListNode(2);
ListNode n3 = new ListNode(5);
ListNode n4 = new ListNode(4);
ListNode n0 = new(1);
ListNode n1 = new(3);
ListNode n2 = new(2);
ListNode n3 = new(5);
ListNode n4 = new(4);
// 构建引用指向
n0.next = n1;
n1.next = n2;
@@ -62,19 +62,19 @@ public class linked_list {
Console.WriteLine($"初始化的链表为{n0}");
// 插入节点
insert(n0, new ListNode(0));
Insert(n0, new ListNode(0));
Console.WriteLine($"插入节点后的链表为{n0}");
// 删除节点
remove(n0);
Remove(n0);
Console.WriteLine($"删除节点后的链表为{n0}");
// 访问节点
ListNode? node = access(n0, 3);
ListNode? node = Access(n0, 3);
Console.WriteLine($"链表中索引 3 处的节点的值 = {node?.val}");
// 查找节点
int index = find(n0, 2);
int index = Find(n0, 2);
Console.WriteLine($"链表中值为 2 的节点的索引 = {index}");
}
}

View File

@@ -11,7 +11,7 @@ class MyList {
private int[] nums; // 数组(存储列表元素)
private int numsCapacity = 10; // 列表容量
private int numsSize = 0; // 列表长度(即当前元素数量)
private int extendRatio = 2; // 每次列表扩容的倍数
private readonly int extendRatio = 2; // 每次列表扩容的倍数
/* 构造方法 */
public MyList() {
@@ -19,17 +19,17 @@ class MyList {
}
/* 获取列表长度(即当前元素数量)*/
public int size() {
public int Size() {
return numsSize;
}
/* 获取列表容量 */
public int capacity() {
public int Capacity() {
return numsCapacity;
}
/* 访问元素 */
public int get(int index) {
public int Get(int index) {
// 索引如果越界则抛出异常,下同
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
@@ -37,29 +37,29 @@ class MyList {
}
/* 更新元素 */
public void set(int index, int num) {
public void Set(int index, int num) {
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
nums[index] = num;
}
/* 尾部添加元素 */
public void add(int num) {
public void Add(int num) {
// 元素数量超出容量时,触发扩容机制
if (numsSize == numsCapacity)
extendCapacity();
ExtendCapacity();
nums[numsSize] = num;
// 更新元素数量
numsSize++;
}
/* 中间插入元素 */
public void insert(int index, int num) {
public void Insert(int index, int num) {
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
// 元素数量超出容量时,触发扩容机制
if (numsSize == numsCapacity)
extendCapacity();
ExtendCapacity();
// 将索引 index 以及之后的元素都向后移动一位
for (int j = numsSize - 1; j >= index; j--) {
nums[j + 1] = nums[j];
@@ -70,7 +70,7 @@ class MyList {
}
/* 删除元素 */
public int remove(int index) {
public int Remove(int index) {
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
int num = nums[index];
@@ -85,7 +85,7 @@ class MyList {
}
/* 列表扩容 */
public void extendCapacity() {
public void ExtendCapacity() {
// 新建一个长度为 numsCapacity * extendRatio 的数组,并将原数组拷贝到新数组
Array.Resize(ref nums, numsCapacity * extendRatio);
// 更新列表容量
@@ -93,11 +93,11 @@ class MyList {
}
/* 将列表转换为数组 */
public int[] toArray() {
public int[] ToArray() {
// 仅转换有效长度范围内的列表元素
int[] nums = new int[numsSize];
for (int i = 0; i < numsSize; i++) {
nums[i] = get(i);
nums[i] = Get(i);
}
return nums;
}
@@ -107,38 +107,38 @@ public class my_list {
[Test]
public void Test() {
/* 初始化列表 */
MyList list = new MyList();
MyList list = new();
/* 尾部添加元素 */
list.add(1);
list.add(3);
list.add(2);
list.add(5);
list.add(4);
Console.WriteLine("列表 list = " + string.Join(",", list.toArray()) +
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
list.Add(1);
list.Add(3);
list.Add(2);
list.Add(5);
list.Add(4);
Console.WriteLine("列表 list = " + string.Join(",", list.ToArray()) +
" ,容量 = " + list.Capacity() + " ,长度 = " + list.Size());
/* 中间插入元素 */
list.insert(3, 6);
Console.WriteLine("在索引 3 处插入数字 6 ,得到 list = " + string.Join(",", list.toArray()));
list.Insert(3, 6);
Console.WriteLine("在索引 3 处插入数字 6 ,得到 list = " + string.Join(",", list.ToArray()));
/* 删除元素 */
list.remove(3);
Console.WriteLine("删除索引 3 处的元素,得到 list = " + string.Join(",", list.toArray()));
list.Remove(3);
Console.WriteLine("删除索引 3 处的元素,得到 list = " + string.Join(",", list.ToArray()));
/* 访问元素 */
int num = list.get(1);
int num = list.Get(1);
Console.WriteLine("访问索引 1 处的元素,得到 num = " + num);
/* 更新元素 */
list.set(1, 0);
Console.WriteLine("将索引 1 处的元素更新为 0 ,得到 list = " + string.Join(",", list.toArray()));
list.Set(1, 0);
Console.WriteLine("将索引 1 处的元素更新为 0 ,得到 list = " + string.Join(",", list.ToArray()));
/* 测试扩容机制 */
for (int i = 0; i < 10; i++) {
// 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
list.add(i);
list.Add(i);
}
Console.WriteLine("扩容后的列表 list = " + string.Join(",", list.toArray()) +
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
Console.WriteLine("扩容后的列表 list = " + string.Join(",", list.ToArray()) +
" ,容量 = " + list.Capacity() + " ,长度 = " + list.Size());
}
}

View File

@@ -8,11 +8,11 @@ namespace hello_algo.chapter_backtracking;
public class n_queens {
/* 回溯算法N 皇后 */
static void backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,
static void Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,
bool[] cols, bool[] diags1, bool[] diags2) {
// 当放置完所有行时,记录解
if (row == n) {
List<List<string>> copyState = new List<List<string>>();
List<List<string>> copyState = new();
foreach (List<string> sRow in state) {
copyState.Add(new List<string>(sRow));
}
@@ -30,7 +30,7 @@ public class n_queens {
state[row][col] = "Q";
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 放置下一行
backtrack(row + 1, n, state, res, cols, diags1, diags2);
Backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 回退:将该格子恢复为空位
state[row][col] = "#";
cols[col] = diags1[diag1] = diags2[diag2] = false;
@@ -39,11 +39,11 @@ public class n_queens {
}
/* 求解 N 皇后 */
static List<List<List<string>>> nQueens(int n) {
static List<List<List<string>>> NQueens(int n) {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
List<List<string>> state = new List<List<string>>();
List<List<string>> state = new();
for (int i = 0; i < n; i++) {
List<string> row = new List<string>();
List<string> row = new();
for (int j = 0; j < n; j++) {
row.Add("#");
}
@@ -52,9 +52,9 @@ public class n_queens {
bool[] cols = new bool[n]; // 记录列是否有皇后
bool[] diags1 = new bool[2 * n - 1]; // 记录主对角线是否有皇后
bool[] diags2 = new bool[2 * n - 1]; // 记录副对角线是否有皇后
List<List<List<string>>> res = new List<List<List<string>>>();
List<List<List<string>>> res = new();
backtrack(0, n, state, res, cols, diags1, diags2);
Backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
@@ -62,7 +62,7 @@ public class n_queens {
[Test]
public void Test() {
int n = 4;
List<List<List<string>>> res = nQueens(n);
List<List<List<string>>> res = NQueens(n);
Console.WriteLine("输入棋盘长宽为 " + n);
Console.WriteLine("皇后放置方案共有 " + res.Count + " 种");

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_backtracking;
public class permutations_i {
/* 回溯算法:全排列 I */
static void backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
static void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
// 当状态长度等于元素数量时,记录解
if (state.Count == choices.Length) {
res.Add(new List<int>(state));
@@ -23,7 +23,7 @@ public class permutations_i {
selected[i] = true;
state.Add(choice);
// 进行下一轮选择
backtrack(state, choices, selected, res);
Backtrack(state, choices, selected, res);
// 回退:撤销选择,恢复到之前的状态
selected[i] = false;
state.RemoveAt(state.Count - 1);
@@ -32,9 +32,9 @@ public class permutations_i {
}
/* 全排列 I */
static List<List<int>> permutationsI(int[] nums) {
List<List<int>> res = new List<List<int>>();
backtrack(new List<int>(), nums, new bool[nums.Length], res);
static List<List<int>> PermutationsI(int[] nums) {
List<List<int>> res = new();
Backtrack(new List<int>(), nums, new bool[nums.Length], res);
return res;
}
@@ -42,7 +42,7 @@ public class permutations_i {
public void Test() {
int[] nums = { 1, 2, 3 };
List<List<int>> res = permutationsI(nums);
List<List<int>> res = PermutationsI(nums);
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums));
Console.WriteLine("所有排列 res = ");

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_backtracking;
public class permutations_ii {
/* 回溯算法:全排列 II */
static void backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
static void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
// 当状态长度等于元素数量时,记录解
if (state.Count == choices.Length) {
res.Add(new List<int>(state));
@@ -25,7 +25,7 @@ public class permutations_ii {
selected[i] = true;
state.Add(choice);
// 进行下一轮选择
backtrack(state, choices, selected, res);
Backtrack(state, choices, selected, res);
// 回退:撤销选择,恢复到之前的状态
selected[i] = false;
state.RemoveAt(state.Count - 1);
@@ -34,9 +34,9 @@ public class permutations_ii {
}
/* 全排列 II */
static List<List<int>> permutationsII(int[] nums) {
List<List<int>> res = new List<List<int>>();
backtrack(new List<int>(), nums, new bool[nums.Length], res);
static List<List<int>> PermutationsII(int[] nums) {
List<List<int>> res = new();
Backtrack(new List<int>(), nums, new bool[nums.Length], res);
return res;
}
@@ -44,7 +44,7 @@ public class permutations_ii {
public void Test() {
int[] nums = { 1, 2, 2 };
List<List<int>> res = permutationsII(nums);
List<List<int>> res = PermutationsII(nums);
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums));
Console.WriteLine("所有排列 res = ");

View File

@@ -10,7 +10,7 @@ public class preorder_traversal_i_compact {
static List<TreeNode> res;
/* 前序遍历:例题一 */
static void preOrder(TreeNode root) {
static void PreOrder(TreeNode root) {
if (root == null) {
return;
}
@@ -18,8 +18,8 @@ public class preorder_traversal_i_compact {
// 记录解
res.Add(root);
}
preOrder(root.left);
preOrder(root.right);
PreOrder(root.left);
PreOrder(root.right);
}
[Test]
@@ -30,7 +30,7 @@ public class preorder_traversal_i_compact {
// 前序遍历
res = new List<TreeNode>();
preOrder(root);
PreOrder(root);
Console.WriteLine("\n输出所有值为 7 的节点");
PrintUtil.PrintList(res.Select(p => p.val).ToList());

View File

@@ -11,7 +11,7 @@ public class preorder_traversal_ii_compact {
static List<List<TreeNode>> res;
/* 前序遍历:例题二 */
static void preOrder(TreeNode root) {
static void PreOrder(TreeNode root) {
if (root == null) {
return;
}
@@ -21,8 +21,8 @@ public class preorder_traversal_ii_compact {
// 记录解
res.Add(new List<TreeNode>(path));
}
preOrder(root.left);
preOrder(root.right);
PreOrder(root.left);
PreOrder(root.right);
// 回退
path.RemoveAt(path.Count - 1);
}
@@ -36,7 +36,7 @@ public class preorder_traversal_ii_compact {
// 前序遍历
path = new List<TreeNode>();
res = new List<List<TreeNode>>();
preOrder(root);
PreOrder(root);
Console.WriteLine("\n输出所有根节点到节点 7 的路径");
foreach (List<TreeNode> path in res) {

View File

@@ -11,7 +11,7 @@ public class preorder_traversal_iii_compact {
static List<List<TreeNode>> res;
/* 前序遍历:例题三 */
static void preOrder(TreeNode root) {
static void PreOrder(TreeNode root) {
// 剪枝
if (root == null || root.val == 3) {
return;
@@ -22,8 +22,8 @@ public class preorder_traversal_iii_compact {
// 记录解
res.Add(new List<TreeNode>(path));
}
preOrder(root.left);
preOrder(root.right);
PreOrder(root.left);
PreOrder(root.right);
// 回退
path.RemoveAt(path.Count - 1);
}
@@ -37,7 +37,7 @@ public class preorder_traversal_iii_compact {
// 前序遍历
path = new List<TreeNode>();
res = new List<List<TreeNode>>();
preOrder(root);
PreOrder(root);
Console.WriteLine("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点");
foreach (List<TreeNode> path in res) {

View File

@@ -8,47 +8,47 @@ namespace hello_algo.chapter_backtracking;
public class preorder_traversal_iii_template {
/* 判断当前状态是否为解 */
static bool isSolution(List<TreeNode> state) {
static bool IsSolution(List<TreeNode> state) {
return state.Count != 0 && state[^1].val == 7;
}
/* 记录解 */
static void recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
static void RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
res.Add(new List<TreeNode>(state));
}
/* 判断在当前状态下,该选择是否合法 */
static bool isValid(List<TreeNode> state, TreeNode choice) {
static bool IsValid(List<TreeNode> state, TreeNode choice) {
return choice != null && choice.val != 3;
}
/* 更新状态 */
static void makeChoice(List<TreeNode> state, TreeNode choice) {
static void MakeChoice(List<TreeNode> state, TreeNode choice) {
state.Add(choice);
}
/* 恢复状态 */
static void undoChoice(List<TreeNode> state, TreeNode choice) {
static void UndoChoice(List<TreeNode> state, TreeNode choice) {
state.RemoveAt(state.Count - 1);
}
/* 回溯算法:例题三 */
static void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
static void Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
// 检查是否为解
if (isSolution(state)) {
if (IsSolution(state)) {
// 记录解
recordSolution(state, res);
RecordSolution(state, res);
}
// 遍历所有选择
foreach (TreeNode choice in choices) {
// 剪枝:检查选择是否合法
if (isValid(state, choice)) {
if (IsValid(state, choice)) {
// 尝试:做出选择,更新状态
makeChoice(state, choice);
MakeChoice(state, choice);
// 进行下一轮选择
backtrack(state, new List<TreeNode> { choice.left, choice.right }, res);
Backtrack(state, new List<TreeNode> { choice.left, choice.right }, res);
// 回退:撤销选择,恢复到之前的状态
undoChoice(state, choice);
UndoChoice(state, choice);
}
}
}
@@ -60,9 +60,9 @@ public class preorder_traversal_iii_template {
PrintUtil.PrintTree(root);
// 回溯算法
List<List<TreeNode>> res = new List<List<TreeNode>>();
List<TreeNode> choices = new List<TreeNode>() { root };
backtrack(new List<TreeNode>(), choices, res);
List<List<TreeNode>> res = new();
List<TreeNode> choices = new() { root };
Backtrack(new List<TreeNode>(), choices, res);
Console.WriteLine("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点");
foreach (List<TreeNode> path in res) {

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_backtracking;
public class subset_sum_i {
/* 回溯算法:子集和 I */
public static void backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {
public static void Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {
// 子集和等于 target 时,记录解
if (target == 0) {
res.Add(new List<int>(state));
@@ -25,19 +25,19 @@ public class subset_sum_i {
// 尝试:做出选择,更新 target, start
state.Add(choices[i]);
// 进行下一轮选择
backtrack(state, target - choices[i], choices, i, res);
Backtrack(state, target - choices[i], choices, i, res);
// 回退:撤销选择,恢复到之前的状态
state.RemoveAt(state.Count - 1);
}
}
/* 求解子集和 I */
public static List<List<int>> subsetSumI(int[] nums, int target) {
List<int> state = new List<int>(); // 状态(子集)
public static List<List<int>> SubsetSumI(int[] nums, int target) {
List<int> state = new(); // 状态(子集)
Array.Sort(nums); // 对 nums 进行排序
int start = 0; // 遍历起始点
List<List<int>> res = new List<List<int>>(); // 结果列表(子集列表)
backtrack(state, target, nums, start, res);
List<List<int>> res = new(); // 结果列表(子集列表)
Backtrack(state, target, nums, start, res);
return res;
}
@@ -45,7 +45,7 @@ public class subset_sum_i {
public void Test() {
int[] nums = { 3, 4, 5 };
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("所有和等于 " + target + " 的子集 res = ");
foreach (var subset in res) {

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_backtracking;
public class subset_sum_i_naive {
/* 回溯算法:子集和 I */
public static void backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {
public static void Backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {
// 子集和等于 target 时,记录解
if (total == target) {
res.Add(new List<int>(state));
@@ -23,18 +23,18 @@ public class subset_sum_i_naive {
// 尝试:做出选择,更新元素和 total
state.Add(choices[i]);
// 进行下一轮选择
backtrack(state, target, total + choices[i], choices, res);
Backtrack(state, target, total + choices[i], choices, res);
// 回退:撤销选择,恢复到之前的状态
state.RemoveAt(state.Count - 1);
}
}
/* 求解子集和 I包含重复子集 */
public static List<List<int>> subsetSumINaive(int[] nums, int target) {
List<int> state = new List<int>(); // 状态(子集)
public static List<List<int>> SubsetSumINaive(int[] nums, int target) {
List<int> state = new(); // 状态(子集)
int total = 0; // 子集和
List<List<int>> res = new List<List<int>>(); // 结果列表(子集列表)
backtrack(state, target, total, nums, res);
List<List<int>> res = new(); // 结果列表(子集列表)
Backtrack(state, target, total, nums, res);
return res;
}
@@ -42,7 +42,7 @@ public class subset_sum_i_naive {
public void Test() {
int[] nums = { 3, 4, 5 };
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("所有和等于 " + target + " 的子集 res = ");
foreach (var subset in res) {

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_backtracking;
public class subset_sum_ii {
/* 回溯算法:子集和 II */
public static void backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {
public static void Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {
// 子集和等于 target 时,记录解
if (target == 0) {
res.Add(new List<int>(state));
@@ -30,19 +30,19 @@ public class subset_sum_ii {
// 尝试:做出选择,更新 target, start
state.Add(choices[i]);
// 进行下一轮选择
backtrack(state, target - choices[i], choices, i + 1, res);
Backtrack(state, target - choices[i], choices, i + 1, res);
// 回退:撤销选择,恢复到之前的状态
state.RemoveAt(state.Count - 1);
}
}
/* 求解子集和 II */
public static List<List<int>> subsetSumII(int[] nums, int target) {
List<int> state = new List<int>(); // 状态(子集)
public static List<List<int>> SubsetSumII(int[] nums, int target) {
List<int> state = new(); // 状态(子集)
Array.Sort(nums); // 对 nums 进行排序
int start = 0; // 遍历起始点
List<List<int>> res = new List<List<int>>(); // 结果列表(子集列表)
backtrack(state, target, nums, start, res);
List<List<int>> res = new(); // 结果列表(子集列表)
Backtrack(state, target, nums, start, res);
return res;
}
@@ -50,7 +50,7 @@ public class subset_sum_ii {
public void Test() {
int[] nums = { 4, 4, 5 };
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("所有和等于 " + target + " 的子集 res = ");
foreach (var subset in res) {

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_computational_complexity;
public class iteration {
/* for 循环 */
public int forLoop(int n) {
public static int ForLoop(int n) {
int res = 0;
// 循环求和 1, 2, ..., n-1, n
for (int i = 1; i <= n; i++) {
@@ -18,7 +18,7 @@ public class iteration {
}
/* while 循环 */
public int whileLoop(int n) {
public static int WhileLoop(int n) {
int res = 0;
int i = 1; // 初始化条件变量
// 循环求和 1, 2, ..., n-1, n
@@ -30,7 +30,7 @@ public class iteration {
}
/* while 循环(两次更新) */
public int whileLoopII(int n) {
public static int WhileLoopII(int n) {
int res = 0;
int i = 1; // 初始化条件变量
// 循环求和 1, 2, 4, 5...
@@ -44,8 +44,8 @@ public class iteration {
}
/* 双层 for 循环 */
public string nestedForLoop(int n) {
StringBuilder res = new StringBuilder();
public static string NestedForLoop(int n) {
StringBuilder res = new();
// 循环 i = 1, 2, ..., n-1, n
for (int i = 1; i <= n; i++) {
// 循环 j = 1, 2, ..., n-1, n
@@ -62,16 +62,16 @@ public class iteration {
int n = 5;
int res;
res = forLoop(n);
res = ForLoop(n);
Console.WriteLine("\nfor 循环的求和结果 res = " + res);
res = whileLoop(n);
res = WhileLoop(n);
Console.WriteLine("\nwhile 循环的求和结果 res = " + res);
res = whileLoopII(n);
res = WhileLoopII(n);
Console.WriteLine("\nwhile 循环(两次更新)求和结果 res = " + res);
string resStr = nestedForLoop(n);
string resStr = NestedForLoop(n);
Console.WriteLine("\n双层 for 循环的遍历结果 " + resStr);
}
}

View File

@@ -8,20 +8,20 @@ namespace hello_algo.chapter_computational_complexity;
public class recursion {
/* 递归 */
public int recur(int n) {
public int Recur(int n) {
// 终止条件
if (n == 1)
return 1;
// 递:递归调用
int res = recur(n - 1);
int res = Recur(n - 1);
// 归:返回结果
return n + res;
}
/* 使用迭代模拟递归 */
public int forLoopRecur(int n) {
public static int ForLoopRecur(int n) {
// 使用一个显式的栈来模拟系统调用栈
Stack<int> stack = new Stack<int>();
Stack<int> stack = new();
int res = 0;
// 递:递归调用
for (int i = n; i > 0; i--) {
@@ -38,21 +38,21 @@ public class recursion {
}
/* 尾递归 */
public int tailRecur(int n, int res) {
public int TailRecur(int n, int res) {
// 终止条件
if (n == 0)
return res;
// 尾递归调用
return tailRecur(n - 1, res + n);
return TailRecur(n - 1, res + n);
}
/* 斐波那契数列:递归 */
public int fib(int n) {
public int Fib(int n) {
// 终止条件 f(1) = 0, f(2) = 1
if (n == 1 || n == 2)
return n - 1;
// 递归调用 f(n) = f(n-1) + f(n-2)
int res = fib(n - 1) + fib(n - 2);
int res = Fib(n - 1) + Fib(n - 2);
// 返回结果 f(n)
return res;
}
@@ -63,16 +63,16 @@ public class recursion {
int n = 5;
int res;
res = recur(n);
res = Recur(n);
Console.WriteLine("\n递归函数的求和结果 res = " + res);
res = forLoopRecur(n);
res = ForLoopRecur(n);
Console.WriteLine("\n使用迭代模拟递归求和结果 res = " + res);
res = tailRecur(n, 0);
res = TailRecur(n, 0);
Console.WriteLine("\n尾递归函数的求和结果 res = " + res);
res = fib(n);
res = Fib(n);
Console.WriteLine("\n斐波那契数列的第 " + n + " 项为 " + res);
}
}

View File

@@ -8,30 +8,30 @@ namespace hello_algo.chapter_computational_complexity;
public class space_complexity {
/* 函数 */
static int function() {
static int Function() {
// 执行某些操作
return 0;
}
/* 常数阶 */
static void constant(int n) {
static void Constant(int n) {
// 常量、变量、对象占用 O(1) 空间
int a = 0;
int b = 0;
int[] nums = new int[10000];
ListNode node = new ListNode(0);
ListNode node = new(0);
// 循环中的变量占用 O(1) 空间
for (int i = 0; i < n; i++) {
int c = 0;
}
// 循环中的函数占用 O(1) 空间
for (int i = 0; i < n; i++) {
function();
Function();
}
}
/* 线性阶 */
static void linear(int n) {
static void Linear(int n) {
// 长度为 n 的数组占用 O(n) 空间
int[] nums = new int[n];
// 长度为 n 的列表占用 O(n) 空间
@@ -47,14 +47,14 @@ public class space_complexity {
}
/* 线性阶(递归实现) */
static void linearRecur(int n) {
static void LinearRecur(int n) {
Console.WriteLine("递归 n = " + n);
if (n == 1) return;
linearRecur(n - 1);
LinearRecur(n - 1);
}
/* 平方阶 */
static void quadratic(int n) {
static void Quadratic(int n) {
// 矩阵占用 O(n^2) 空间
int[,] numMatrix = new int[n, n];
// 二维列表占用 O(n^2) 空间
@@ -69,19 +69,20 @@ public class space_complexity {
}
/* 平方阶(递归实现) */
static int quadraticRecur(int n) {
static int QuadraticRecur(int n) {
if (n <= 0) return 0;
int[] nums = new int[n];
Console.WriteLine("递归 n = " + n + " 中的 nums 长度 = " + nums.Length);
return quadraticRecur(n - 1);
return QuadraticRecur(n - 1);
}
/* 指数阶(建立满二叉树) */
static TreeNode? buildTree(int n) {
static TreeNode? BuildTree(int n) {
if (n == 0) return null;
TreeNode root = new TreeNode(0);
root.left = buildTree(n - 1);
root.right = buildTree(n - 1);
TreeNode root = new(0) {
left = BuildTree(n - 1),
right = BuildTree(n - 1)
};
return root;
}
@@ -89,15 +90,15 @@ public class space_complexity {
public void Test() {
int n = 5;
// 常数阶
constant(n);
Constant(n);
// 线性阶
linear(n);
linearRecur(n);
Linear(n);
LinearRecur(n);
// 平方阶
quadratic(n);
quadraticRecur(n);
Quadratic(n);
QuadraticRecur(n);
// 指数阶
TreeNode? root = buildTree(n);
TreeNode? root = BuildTree(n);
PrintUtil.PrintTree(root);
}
}

View File

@@ -7,7 +7,7 @@
namespace hello_algo.chapter_computational_complexity;
public class time_complexity {
void algorithm(int n) {
void Algorithm(int n) {
int a = 1; // +0技巧 1
a = a + n; // +0技巧 1
// +n技巧 2
@@ -23,24 +23,24 @@ public class time_complexity {
}
// 算法 A 时间复杂度:常数阶
void algorithm_A(int n) {
void AlgorithmA(int n) {
Console.WriteLine(0);
}
// 算法 B 时间复杂度:线性阶
void algorithm_B(int n) {
void AlgorithmB(int n) {
for (int i = 0; i < n; i++) {
Console.WriteLine(0);
}
}
// 算法 C 时间复杂度:常数阶
void algorithm_C(int n) {
void AlgorithmC(int n) {
for (int i = 0; i < 1000000; i++) {
Console.WriteLine(0);
}
}
/* 常数阶 */
static int constant(int n) {
static int Constant(int n) {
int count = 0;
int size = 100000;
for (int i = 0; i < size; i++)
@@ -49,7 +49,7 @@ public class time_complexity {
}
/* 线性阶 */
static int linear(int n) {
static int Linear(int n) {
int count = 0;
for (int i = 0; i < n; i++)
count++;
@@ -57,7 +57,7 @@ public class time_complexity {
}
/* 线性阶(遍历数组) */
static int arrayTraversal(int[] nums) {
static int ArrayTraversal(int[] nums) {
int count = 0;
// 循环次数与数组长度成正比
foreach (int num in nums) {
@@ -67,7 +67,7 @@ public class time_complexity {
}
/* 平方阶 */
static int quadratic(int n) {
static int Quadratic(int n) {
int count = 0;
// 循环次数与数组长度成平方关系
for (int i = 0; i < n; i++) {
@@ -79,7 +79,7 @@ public class time_complexity {
}
/* 平方阶(冒泡排序) */
static int bubbleSort(int[] nums) {
static int BubbleSort(int[] nums) {
int count = 0; // 计数器
// 外循环:未排序区间为 [0, i]
for (int i = nums.Length - 1; i > 0; i--) {
@@ -96,7 +96,7 @@ public class time_complexity {
}
/* 指数阶(循环实现) */
static int exponential(int n) {
static int Exponential(int n) {
int count = 0, bas = 1;
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (int i = 0; i < n; i++) {
@@ -110,13 +110,13 @@ public class time_complexity {
}
/* 指数阶(递归实现) */
static int expRecur(int n) {
static int ExpRecur(int n) {
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) {
static int Logarithmic(float n) {
int count = 0;
while (n > 1) {
n = n / 2;
@@ -126,16 +126,16 @@ public class time_complexity {
}
/* 对数阶(递归实现) */
static int logRecur(float n) {
static int LogRecur(float n) {
if (n <= 1) return 0;
return logRecur(n / 2) + 1;
return LogRecur(n / 2) + 1;
}
/* 线性对数阶 */
static int linearLogRecur(float n) {
static int LinearLogRecur(float n) {
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++) {
count++;
}
@@ -143,12 +143,12 @@ public class time_complexity {
}
/* 阶乘阶(递归实现) */
static int factorialRecur(int n) {
static int FactorialRecur(int n) {
if (n == 0) return 1;
int count = 0;
// 从 1 个分裂出 n 个
for (int i = 0; i < n; i++) {
count += factorialRecur(n - 1);
count += FactorialRecur(n - 1);
}
return count;
}
@@ -159,36 +159,36 @@ public class time_complexity {
int n = 8;
Console.WriteLine("输入数据大小 n = " + n);
int count = constant(n);
int count = Constant(n);
Console.WriteLine("常数阶的操作数量 = " + count);
count = linear(n);
count = Linear(n);
Console.WriteLine("线性阶的操作数量 = " + count);
count = arrayTraversal(new int[n]);
count = ArrayTraversal(new int[n]);
Console.WriteLine("线性阶(遍历数组)的操作数量 = " + count);
count = quadratic(n);
count = Quadratic(n);
Console.WriteLine("平方阶的操作数量 = " + count);
int[] nums = new int[n];
for (int i = 0; i < n; i++)
nums[i] = n - i; // [n,n-1,...,2,1]
count = bubbleSort(nums);
count = BubbleSort(nums);
Console.WriteLine("平方阶(冒泡排序)的操作数量 = " + count);
count = exponential(n);
count = Exponential(n);
Console.WriteLine("指数阶(循环实现)的操作数量 = " + count);
count = expRecur(n);
count = ExpRecur(n);
Console.WriteLine("指数阶(递归实现)的操作数量 = " + count);
count = logarithmic((float)n);
count = Logarithmic((float)n);
Console.WriteLine("对数阶(循环实现)的操作数量 = " + count);
count = logRecur((float)n);
count = LogRecur((float)n);
Console.WriteLine("对数阶(递归实现)的操作数量 = " + count);
count = linearLogRecur((float)n);
count = LinearLogRecur((float)n);
Console.WriteLine("线性对数阶(递归实现)的操作数量 = " + count);
count = factorialRecur(n);
count = FactorialRecur(n);
Console.WriteLine("阶乘阶(递归实现)的操作数量 = " + count);
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_computational_complexity;
public class worst_best_time_complexity {
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
static int[] randomNumbers(int n) {
static int[] RandomNumbers(int n) {
int[] nums = new int[n];
// 生成数组 nums = { 1, 2, 3, ..., n }
for (int i = 0; i < n; i++) {
@@ -27,7 +27,7 @@ public class worst_best_time_complexity {
}
/* 查找数组 nums 中数字 1 所在索引 */
static int findOne(int[] nums) {
static int FindOne(int[] nums) {
for (int i = 0; i < nums.Length; i++) {
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
@@ -43,8 +43,8 @@ public class worst_best_time_complexity {
public void Test() {
for (int i = 0; i < 10; i++) {
int n = 100;
int[] nums = randomNumbers(n);
int index = findOne(nums);
int[] nums = RandomNumbers(n);
int index = FindOne(nums);
Console.WriteLine("\n数组 [ 1, 2, ..., n ] 被打乱后 = " + string.Join(",", nums));
Console.WriteLine("数字 1 的索引为 " + index);
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_divide_and_conquer;
public class binary_search_recur {
/* 二分查找:问题 f(i, j) */
public int dfs(int[] nums, int target, int i, int j) {
public int Dfs(int[] nums, int target, int i, int j) {
// 若区间为空,代表无目标元素,则返回 -1
if (i > j) {
return -1;
@@ -17,10 +17,10 @@ public class binary_search_recur {
int m = (i + j) / 2;
if (nums[m] < target) {
// 递归子问题 f(m+1, j)
return dfs(nums, target, m + 1, j);
return Dfs(nums, target, m + 1, j);
} else if (nums[m] > target) {
// 递归子问题 f(i, m-1)
return dfs(nums, target, i, m - 1);
return Dfs(nums, target, i, m - 1);
} else {
// 找到目标元素,返回其索引
return m;
@@ -28,10 +28,10 @@ public class binary_search_recur {
}
/* 二分查找 */
public int binarySearch(int[] nums, int target) {
public int BinarySearch(int[] nums, int target) {
int n = nums.Length;
// 求解问题 f(0, n-1)
return dfs(nums, target, 0, n - 1);
return Dfs(nums, target, 0, n - 1);
}
[Test]
@@ -40,7 +40,7 @@ public class binary_search_recur {
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
// 二分查找(双闭区间)
int index = binarySearch(nums, target);
int index = BinarySearch(nums, target);
Console.WriteLine("目标元素 6 的索引 = " + index);
}
}

View File

@@ -8,30 +8,30 @@ namespace hello_algo.chapter_divide_and_conquer;
public class build_tree {
/* 构建二叉树:分治 */
public TreeNode dfs(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {
public TreeNode Dfs(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {
// 子树区间为空时终止
if (r - l < 0)
return null;
// 初始化根节点
TreeNode root = new TreeNode(preorder[i]);
TreeNode root = new(preorder[i]);
// 查询 m ,从而划分左右子树
int m = inorderMap[preorder[i]];
// 子问题:构建左子树
root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
root.left = Dfs(preorder, inorderMap, i + 1, l, m - 1);
// 子问题:构建右子树
root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
root.right = Dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// 返回根节点
return root;
}
/* 构建二叉树 */
public TreeNode buildTree(int[] preorder, int[] inorder) {
public TreeNode BuildTree(int[] preorder, int[] inorder) {
// 初始化哈希表,存储 inorder 元素到索引的映射
Dictionary<int, int> inorderMap = new Dictionary<int, int>();
Dictionary<int, int> inorderMap = new();
for (int i = 0; i < inorder.Length; 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;
}
@@ -42,7 +42,7 @@ public class build_tree {
Console.WriteLine("前序遍历 = " + string.Join(", ", preorder));
Console.WriteLine("中序遍历 = " + string.Join(", ", inorder));
TreeNode root = buildTree(preorder, inorder);
TreeNode root = BuildTree(preorder, inorder);
Console.WriteLine("构建的二叉树为:");
PrintUtil.PrintTree(root);
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_divide_and_conquer;
public class hanota {
/* 移动一个圆盘 */
public void move(List<int> src, List<int> tar) {
public void Move(List<int> src, List<int> tar) {
// 从 src 顶部拿出一个圆盘
int pan = src[^1];
src.RemoveAt(src.Count - 1);
@@ -17,39 +17,39 @@ public class hanota {
}
/* 求解汉诺塔:问题 f(i) */
public void dfs(int i, List<int> src, List<int> buf, List<int> tar) {
public void Dfs(int i, List<int> src, List<int> buf, List<int> tar) {
// 若 src 只剩下一个圆盘,则直接将其移到 tar
if (i == 1) {
move(src, tar);
Move(src, tar);
return;
}
// 子问题 f(i-1) :将 src 顶部 i-1 个圆盘借助 tar 移到 buf
dfs(i - 1, src, tar, buf);
Dfs(i - 1, src, tar, buf);
// 子问题 f(1) :将 src 剩余一个圆盘移到 tar
move(src, tar);
Move(src, tar);
// 子问题 f(i-1) :将 buf 顶部 i-1 个圆盘借助 src 移到 tar
dfs(i - 1, buf, src, tar);
Dfs(i - 1, buf, src, tar);
}
/* 求解汉诺塔 */
public void solveHanota(List<int> A, List<int> B, List<int> C) {
public void SolveHanota(List<int> A, List<int> B, List<int> C) {
int n = A.Count;
// 将 A 顶部 n 个圆盘借助 B 移到 C
dfs(n, A, B, C);
Dfs(n, A, B, C);
}
[Test]
public void Test() {
// 列表尾部是柱子顶部
List<int> A = new List<int> { 5, 4, 3, 2, 1 };
List<int> B = new List<int>();
List<int> C = new List<int>();
List<int> A = new() { 5, 4, 3, 2, 1 };
List<int> B = new();
List<int> C = new();
Console.WriteLine("初始状态下:");
Console.WriteLine("A = " + string.Join(", ", A));
Console.WriteLine("B = " + string.Join(", ", B));
Console.WriteLine("C = " + string.Join(", ", C));
solveHanota(A, B, C);
SolveHanota(A, B, C);
Console.WriteLine("圆盘移动完成后:");
Console.WriteLine("A = " + string.Join(", ", A));

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class climbing_stairs_backtrack {
/* 回溯 */
public void backtrack(List<int> choices, int state, int n, List<int> res) {
public void Backtrack(List<int> choices, int state, int n, List<int> res) {
// 当爬到第 n 阶时,方案数量加 1
if (state == n)
res[0]++;
@@ -18,24 +18,24 @@ public class climbing_stairs_backtrack {
if (state + choice > n)
break;
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);
Backtrack(choices, state + choice, n, res);
// 回退
}
}
/* 爬楼梯:回溯 */
public int climbingStairsBacktrack(int n) {
List<int> choices = new List<int> { 1, 2 }; // 可选择向上爬 1 或 2 阶
public int ClimbingStairsBacktrack(int n) {
List<int> choices = new() { 1, 2 }; // 可选择向上爬 1 或 2 阶
int state = 0; // 从第 0 阶开始爬
List<int> res = new List<int> { 0 }; // 使用 res[0] 记录方案数量
backtrack(choices, state, n, res);
List<int> res = new() { 0 }; // 使用 res[0] 记录方案数量
Backtrack(choices, state, n, res);
return res[0];
}
[Test]
public void Test() {
int n = 9;
int res = climbingStairsBacktrack(n);
int res = ClimbingStairsBacktrack(n);
Console.WriteLine($"爬 {n} 阶楼梯共有 {res} 种方案");
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class climbing_stairs_constraint_dp {
/* 带约束爬楼梯:动态规划 */
public int climbingStairsConstraintDP(int n) {
public int ClimbingStairsConstraintDP(int n) {
if (n == 1 || n == 2) {
return 1;
}
@@ -30,7 +30,7 @@ public class climbing_stairs_constraint_dp {
[Test]
public void Test() {
int n = 9;
int res = climbingStairsConstraintDP(n);
int res = ClimbingStairsConstraintDP(n);
Console.WriteLine($"爬 {n} 阶楼梯共有 {res} 种方案");
}
}

View File

@@ -8,24 +8,24 @@ namespace hello_algo.chapter_dynamic_programming;
public class climbing_stairs_dfs {
/* 搜索 */
public int dfs(int i) {
public int Dfs(int i) {
// 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 || i == 2)
return i;
// dp[i] = dp[i-1] + dp[i-2]
int count = dfs(i - 1) + dfs(i - 2);
int count = Dfs(i - 1) + Dfs(i - 2);
return count;
}
/* 爬楼梯:搜索 */
public int climbingStairsDFS(int n) {
return dfs(n);
public int ClimbingStairsDFS(int n) {
return Dfs(n);
}
[Test]
public void Test() {
int n = 9;
int res = climbingStairsDFS(n);
int res = ClimbingStairsDFS(n);
Console.WriteLine($"爬 {n} 阶楼梯共有 {res} 种方案");
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class climbing_stairs_dfs_mem {
/* 记忆化搜索 */
public int dfs(int i, int[] mem) {
public int Dfs(int i, int[] mem) {
// 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 || i == 2)
return i;
@@ -16,24 +16,24 @@ public class climbing_stairs_dfs_mem {
if (mem[i] != -1)
return mem[i];
// dp[i] = dp[i-1] + dp[i-2]
int count = dfs(i - 1, mem) + dfs(i - 2, mem);
int count = Dfs(i - 1, mem) + Dfs(i - 2, mem);
// 记录 dp[i]
mem[i] = count;
return count;
}
/* 爬楼梯:记忆化搜索 */
public int climbingStairsDFSMem(int n) {
public int ClimbingStairsDFSMem(int n) {
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
int[] mem = new int[n + 1];
Array.Fill(mem, -1);
return dfs(n, mem);
return Dfs(n, mem);
}
[Test]
public void Test() {
int n = 9;
int res = climbingStairsDFSMem(n);
int res = ClimbingStairsDFSMem(n);
Console.WriteLine($"爬 {n} 阶楼梯共有 {res} 种方案");
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class climbing_stairs_dp {
/* 爬楼梯:动态规划 */
public int climbingStairsDP(int n) {
public int ClimbingStairsDP(int n) {
if (n == 1 || n == 2)
return n;
// 初始化 dp 表,用于存储子问题的解
@@ -24,7 +24,7 @@ public class climbing_stairs_dp {
}
/* 爬楼梯:空间优化后的动态规划 */
public int climbingStairsDPComp(int n) {
public int ClimbingStairsDPComp(int n) {
if (n == 1 || n == 2)
return n;
int a = 1, b = 2;
@@ -40,10 +40,10 @@ public class climbing_stairs_dp {
public void Test() {
int n = 9;
int res = climbingStairsDP(n);
int res = ClimbingStairsDP(n);
Console.WriteLine($"爬 {n} 阶楼梯共有 {res} 种方案");
res = climbingStairsDPComp(n);
res = ClimbingStairsDPComp(n);
Console.WriteLine($"爬 {n} 阶楼梯共有 {res} 种方案");
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class coin_change {
/* 零钱兑换:动态规划 */
public int coinChangeDP(int[] coins, int amt) {
public int CoinChangeDP(int[] coins, int amt) {
int n = coins.Length;
int MAX = amt + 1;
// 初始化 dp 表
@@ -33,7 +33,7 @@ public class coin_change {
}
/* 零钱兑换:空间优化后的动态规划 */
public int coinChangeDPComp(int[] coins, int amt) {
public int CoinChangeDPComp(int[] coins, int amt) {
int n = coins.Length;
int MAX = amt + 1;
// 初始化 dp 表
@@ -61,11 +61,11 @@ public class coin_change {
int amt = 4;
// 动态规划
int res = coinChangeDP(coins, amt);
int res = CoinChangeDP(coins, amt);
Console.WriteLine("凑到目标金额所需的最少硬币数量为 " + res);
// 空间优化后的动态规划
res = coinChangeDPComp(coins, amt);
res = CoinChangeDPComp(coins, amt);
Console.WriteLine("凑到目标金额所需的最少硬币数量为 " + res);
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class coin_change_ii {
/* 零钱兑换 II动态规划 */
public int coinChangeIIDP(int[] coins, int amt) {
public int CoinChangeIIDP(int[] coins, int amt) {
int n = coins.Length;
// 初始化 dp 表
int[,] dp = new int[n + 1, amt + 1];
@@ -32,7 +32,7 @@ public class coin_change_ii {
}
/* 零钱兑换 II空间优化后的动态规划 */
public int coinChangeIIDPComp(int[] coins, int amt) {
public int CoinChangeIIDPComp(int[] coins, int amt) {
int n = coins.Length;
// 初始化 dp 表
int[] dp = new int[amt + 1];
@@ -58,11 +58,11 @@ public class coin_change_ii {
int amt = 5;
// 动态规划
int res = coinChangeIIDP(coins, amt);
int res = CoinChangeIIDP(coins, amt);
Console.WriteLine("凑出目标金额的硬币组合数量为 " + res);
// 空间优化后的动态规划
res = coinChangeIIDPComp(coins, amt);
res = CoinChangeIIDPComp(coins, amt);
Console.WriteLine("凑出目标金额的硬币组合数量为 " + res);
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class edit_distance {
/* 编辑距离:暴力搜索 */
public int editDistanceDFS(string s, string t, int i, int j) {
public int EditDistanceDFS(string s, string t, int i, int j) {
// 若 s 和 t 都为空,则返回 0
if (i == 0 && j == 0)
return 0;
@@ -20,17 +20,17 @@ public class edit_distance {
return i;
// 若两字符相等,则直接跳过此两字符
if (s[i - 1] == t[j - 1])
return editDistanceDFS(s, t, i - 1, j - 1);
return EditDistanceDFS(s, t, i - 1, j - 1);
// 最少编辑步数 = 插入、删除、替换这三种操作的最少编辑步数 + 1
int insert = editDistanceDFS(s, t, i, j - 1);
int delete = editDistanceDFS(s, t, i - 1, j);
int replace = editDistanceDFS(s, t, i - 1, j - 1);
int insert = EditDistanceDFS(s, t, i, j - 1);
int delete = EditDistanceDFS(s, t, i - 1, j);
int replace = EditDistanceDFS(s, t, i - 1, j - 1);
// 返回最少编辑步数
return Math.Min(Math.Min(insert, delete), replace) + 1;
}
/* 编辑距离:记忆化搜索 */
public int editDistanceDFSMem(string s, string t, int[][] mem, int i, int j) {
public int EditDistanceDFSMem(string s, string t, int[][] mem, int i, int j) {
// 若 s 和 t 都为空,则返回 0
if (i == 0 && j == 0)
return 0;
@@ -45,18 +45,18 @@ public class edit_distance {
return mem[i][j];
// 若两字符相等,则直接跳过此两字符
if (s[i - 1] == t[j - 1])
return editDistanceDFSMem(s, t, mem, i - 1, j - 1);
return EditDistanceDFSMem(s, t, mem, i - 1, j - 1);
// 最少编辑步数 = 插入、删除、替换这三种操作的最少编辑步数 + 1
int insert = editDistanceDFSMem(s, t, mem, i, j - 1);
int delete = editDistanceDFSMem(s, t, mem, i - 1, j);
int replace = editDistanceDFSMem(s, t, mem, i - 1, j - 1);
int insert = EditDistanceDFSMem(s, t, mem, i, j - 1);
int delete = EditDistanceDFSMem(s, t, mem, i - 1, j);
int replace = EditDistanceDFSMem(s, t, mem, i - 1, j - 1);
// 记录并返回最少编辑步数
mem[i][j] = Math.Min(Math.Min(insert, delete), replace) + 1;
return mem[i][j];
}
/* 编辑距离:动态规划 */
public int editDistanceDP(string s, string t) {
public int EditDistanceDP(string s, string t) {
int n = s.Length, m = t.Length;
int[,] dp = new int[n + 1, m + 1];
// 状态转移:首行首列
@@ -82,7 +82,7 @@ public class edit_distance {
}
/* 编辑距离:空间优化后的动态规划 */
public int editDistanceDPComp(string s, string t) {
public int EditDistanceDPComp(string s, string t) {
int n = s.Length, m = t.Length;
int[] dp = new int[m + 1];
// 状态转移:首行
@@ -117,7 +117,7 @@ public class edit_distance {
int n = s.Length, m = t.Length;
// 暴力搜索
int res = editDistanceDFS(s, t, n, m);
int res = EditDistanceDFS(s, t, n, m);
Console.WriteLine("将 " + s + " 更改为 " + t + " 最少需要编辑 " + res + " 步");
// 记忆化搜索
@@ -127,15 +127,15 @@ public class edit_distance {
Array.Fill(mem[i], -1);
}
res = editDistanceDFSMem(s, t, mem, n, m);
res = EditDistanceDFSMem(s, t, mem, n, m);
Console.WriteLine("将 " + s + " 更改为 " + t + " 最少需要编辑 " + res + " 步");
// 动态规划
res = editDistanceDP(s, t);
res = EditDistanceDP(s, t);
Console.WriteLine("将 " + s + " 更改为 " + t + " 最少需要编辑 " + res + " 步");
// 空间优化后的动态规划
res = editDistanceDPComp(s, t);
res = EditDistanceDPComp(s, t);
Console.WriteLine("将 " + s + " 更改为 " + t + " 最少需要编辑 " + res + " 步");
}
}

View File

@@ -8,24 +8,24 @@ namespace hello_algo.chapter_dynamic_programming;
public class knapsack {
/* 0-1 背包:暴力搜索 */
public int knapsackDFS(int[] weight, int[] val, int i, int c) {
public int KnapsackDFS(int[] weight, int[] val, int i, int c) {
// 若已选完所有物品或背包无容量,则返回价值 0
if (i == 0 || c == 0) {
return 0;
}
// 若超过背包容量,则只能不放入背包
if (weight[i - 1] > c) {
return knapsackDFS(weight, val, i - 1, c);
return KnapsackDFS(weight, val, i - 1, c);
}
// 计算不放入和放入物品 i 的最大价值
int no = knapsackDFS(weight, val, i - 1, c);
int yes = knapsackDFS(weight, val, i - 1, c - weight[i - 1]) + val[i - 1];
int no = KnapsackDFS(weight, val, i - 1, c);
int yes = KnapsackDFS(weight, val, i - 1, c - weight[i - 1]) + val[i - 1];
// 返回两种方案中价值更大的那一个
return Math.Max(no, yes);
}
/* 0-1 背包:记忆化搜索 */
public int knapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {
public int KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {
// 若已选完所有物品或背包无容量,则返回价值 0
if (i == 0 || c == 0) {
return 0;
@@ -36,18 +36,18 @@ public class knapsack {
}
// 若超过背包容量,则只能不放入背包
if (weight[i - 1] > c) {
return knapsackDFSMem(weight, val, mem, i - 1, c);
return KnapsackDFSMem(weight, val, mem, i - 1, c);
}
// 计算不放入和放入物品 i 的最大价值
int no = knapsackDFSMem(weight, val, mem, i - 1, c);
int yes = knapsackDFSMem(weight, val, mem, i - 1, c - weight[i - 1]) + val[i - 1];
int no = KnapsackDFSMem(weight, val, mem, i - 1, c);
int yes = KnapsackDFSMem(weight, val, mem, i - 1, c - weight[i - 1]) + val[i - 1];
// 记录并返回两种方案中价值更大的那一个
mem[i][c] = Math.Max(no, yes);
return mem[i][c];
}
/* 0-1 背包:动态规划 */
public int knapsackDP(int[] weight, int[] val, int cap) {
public int KnapsackDP(int[] weight, int[] val, int cap) {
int n = weight.Length;
// 初始化 dp 表
int[,] dp = new int[n + 1, cap + 1];
@@ -67,7 +67,7 @@ public class knapsack {
}
/* 0-1 背包:空间优化后的动态规划 */
public int knapsackDPComp(int[] weight, int[] val, int cap) {
public int KnapsackDPComp(int[] weight, int[] val, int cap) {
int n = weight.Length;
// 初始化 dp 表
int[] dp = new int[cap + 1];
@@ -95,7 +95,7 @@ public class knapsack {
int n = weight.Length;
// 暴力搜索
int res = knapsackDFS(weight, val, n, cap);
int res = KnapsackDFS(weight, val, n, cap);
Console.WriteLine("不超过背包容量的最大物品价值为 " + res);
// 记忆化搜索
@@ -104,15 +104,15 @@ public class knapsack {
mem[i] = new int[cap + 1];
Array.Fill(mem[i], -1);
}
res = knapsackDFSMem(weight, val, mem, n, cap);
res = KnapsackDFSMem(weight, val, mem, n, cap);
Console.WriteLine("不超过背包容量的最大物品价值为 " + res);
// 动态规划
res = knapsackDP(weight, val, cap);
res = KnapsackDP(weight, val, cap);
Console.WriteLine("不超过背包容量的最大物品价值为 " + res);
// 空间优化后的动态规划
res = knapsackDPComp(weight, val, cap);
res = KnapsackDPComp(weight, val, cap);
Console.WriteLine("不超过背包容量的最大物品价值为 " + res);
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class min_cost_climbing_stairs_dp {
/* 爬楼梯最小代价:动态规划 */
public int minCostClimbingStairsDP(int[] cost) {
public int MinCostClimbingStairsDP(int[] cost) {
int n = cost.Length - 1;
if (n == 1 || n == 2)
return cost[n];
@@ -25,7 +25,7 @@ public class min_cost_climbing_stairs_dp {
}
/* 爬楼梯最小代价:空间优化后的动态规划 */
public int minCostClimbingStairsDPComp(int[] cost) {
public int MinCostClimbingStairsDPComp(int[] cost) {
int n = cost.Length - 1;
if (n == 1 || n == 2)
return cost[n];
@@ -44,10 +44,10 @@ public class min_cost_climbing_stairs_dp {
Console.WriteLine("输入楼梯的代价列表为");
PrintUtil.PrintList(cost);
int res = minCostClimbingStairsDP(cost);
int res = MinCostClimbingStairsDP(cost);
Console.WriteLine($"爬完楼梯的最低代价为 {res}");
res = minCostClimbingStairsDPComp(cost);
res = MinCostClimbingStairsDPComp(cost);
Console.WriteLine($"爬完楼梯的最低代价为 {res}");
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class min_path_sum {
/* 最小路径和:暴力搜索 */
public int minPathSumDFS(int[][] grid, int i, int j) {
public int MinPathSumDFS(int[][] grid, int i, int j) {
// 若为左上角单元格,则终止搜索
if (i == 0 && j == 0) {
return grid[0][0];
@@ -18,14 +18,14 @@ public class min_path_sum {
return int.MaxValue;
}
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
int left = minPathSumDFS(grid, i - 1, j);
int up = minPathSumDFS(grid, i, j - 1);
int left = MinPathSumDFS(grid, i - 1, j);
int up = MinPathSumDFS(grid, i, j - 1);
// 返回从左上角到 (i, j) 的最小路径代价
return Math.Min(left, up) + grid[i][j];
}
/* 最小路径和:记忆化搜索 */
public int minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {
public int MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {
// 若为左上角单元格,则终止搜索
if (i == 0 && j == 0) {
return grid[0][0];
@@ -39,15 +39,15 @@ public class min_path_sum {
return mem[i][j];
}
// 左边和上边单元格的最小路径代价
int left = minPathSumDFSMem(grid, mem, i - 1, j);
int up = minPathSumDFSMem(grid, mem, i, j - 1);
int left = MinPathSumDFSMem(grid, mem, i - 1, j);
int up = MinPathSumDFSMem(grid, mem, i, j - 1);
// 记录并返回左上角到 (i, j) 的最小路径代价
mem[i][j] = Math.Min(left, up) + grid[i][j];
return mem[i][j];
}
/* 最小路径和:动态规划 */
public int minPathSumDP(int[][] grid) {
public int MinPathSumDP(int[][] grid) {
int n = grid.Length, m = grid[0].Length;
// 初始化 dp 表
int[,] dp = new int[n, m];
@@ -70,7 +70,7 @@ public class min_path_sum {
}
/* 最小路径和:空间优化后的动态规划 */
public int minPathSumDPComp(int[][] grid) {
public int MinPathSumDPComp(int[][] grid) {
int n = grid.Length, m = grid[0].Length;
// 初始化 dp 表
int[] dp = new int[m];
@@ -104,7 +104,7 @@ public class min_path_sum {
int n = grid.Length, m = grid[0].Length;
// 暴力搜索
int res = minPathSumDFS(grid, n - 1, m - 1);
int res = MinPathSumDFS(grid, n - 1, m - 1);
Console.WriteLine("从左上角到右下角的做小路径和为 " + res);
// 记忆化搜索
@@ -113,15 +113,15 @@ public class min_path_sum {
mem[i] = new int[m];
Array.Fill(mem[i], -1);
}
res = minPathSumDFSMem(grid, mem, n - 1, m - 1);
res = MinPathSumDFSMem(grid, mem, n - 1, m - 1);
Console.WriteLine("从左上角到右下角的做小路径和为 " + res);
// 动态规划
res = minPathSumDP(grid);
res = MinPathSumDP(grid);
Console.WriteLine("从左上角到右下角的做小路径和为 " + res);
// 空间优化后的动态规划
res = minPathSumDPComp(grid);
res = MinPathSumDPComp(grid);
Console.WriteLine("从左上角到右下角的做小路径和为 " + res);
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
public class unbounded_knapsack {
/* 完全背包:动态规划 */
public int unboundedKnapsackDP(int[] wgt, int[] val, int cap) {
public int UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {
int n = wgt.Length;
// 初始化 dp 表
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) {
public int UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {
int n = wgt.Length;
// 初始化 dp 表
int[] dp = new int[cap + 1];
@@ -54,11 +54,11 @@ public class unbounded_knapsack {
int cap = 4;
// 动态规划
int res = unboundedKnapsackDP(wgt, val, cap);
int res = UnboundedKnapsackDP(wgt, val, cap);
Console.WriteLine("不超过背包容量的最大物品价值为 " + res);
// 空间优化后的动态规划
res = unboundedKnapsackDPComp(wgt, val, cap);
res = UnboundedKnapsackDPComp(wgt, val, cap);
Console.WriteLine("不超过背包容量的最大物品价值为 " + res);
}
}

View File

@@ -16,19 +16,19 @@ public class GraphAdjList {
this.adjList = new Dictionary<Vertex, List<Vertex>>();
// 添加所有顶点和边
foreach (Vertex[] edge in edges) {
addVertex(edge[0]);
addVertex(edge[1]);
addEdge(edge[0], edge[1]);
AddVertex(edge[0]);
AddVertex(edge[1]);
AddEdge(edge[0], edge[1]);
}
}
/* 获取顶点数量 */
public int size() {
public int Size() {
return adjList.Count;
}
/* 添加边 */
public void addEdge(Vertex vet1, Vertex vet2) {
public void AddEdge(Vertex vet1, Vertex vet2) {
if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)
throw new InvalidOperationException();
// 添加边 vet1 - vet2
@@ -37,7 +37,7 @@ public class GraphAdjList {
}
/* 删除边 */
public void removeEdge(Vertex vet1, Vertex vet2) {
public void RemoveEdge(Vertex vet1, Vertex vet2) {
if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)
throw new InvalidOperationException();
// 删除边 vet1 - vet2
@@ -46,7 +46,7 @@ public class GraphAdjList {
}
/* 添加顶点 */
public void addVertex(Vertex vet) {
public void AddVertex(Vertex vet) {
if (adjList.ContainsKey(vet))
return;
// 在邻接表中添加一个新链表
@@ -54,7 +54,7 @@ public class GraphAdjList {
}
/* 删除顶点 */
public void removeVertex(Vertex vet) {
public void RemoveVertex(Vertex vet) {
if (!adjList.ContainsKey(vet))
throw new InvalidOperationException();
// 在邻接表中删除顶点 vet 对应的链表
@@ -66,10 +66,10 @@ public class GraphAdjList {
}
/* 打印邻接表 */
public void print() {
public void Print() {
Console.WriteLine("邻接表 =");
foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {
List<int> tmp = new List<int>();
List<int> tmp = new();
foreach (Vertex vertex in pair.Value)
tmp.Add(vertex.val);
Console.WriteLine(pair.Key.val + ": [" + string.Join(", ", tmp) + "],");
@@ -85,32 +85,32 @@ public class graph_adjacency_list {
Vertex[][] edges = new Vertex[][] { new Vertex[] { v[0], v[1] }, new Vertex[] { v[0], v[3] },
new Vertex[] { v[1], v[2] }, new Vertex[] { v[2], v[3] },
new Vertex[] { v[2], v[4] }, new Vertex[] { v[3], v[4] } };
GraphAdjList graph = new GraphAdjList(edges);
GraphAdjList graph = new(edges);
Console.WriteLine("\n初始化后图为");
graph.print();
graph.Print();
/* 添加边 */
// 顶点 1, 2 即 v[0], v[2]
graph.addEdge(v[0], v[2]);
graph.AddEdge(v[0], v[2]);
Console.WriteLine("\n添加边 1-2 后,图为");
graph.print();
graph.Print();
/* 删除边 */
// 顶点 1, 3 即 v[0], v[1]
graph.removeEdge(v[0], v[1]);
graph.RemoveEdge(v[0], v[1]);
Console.WriteLine("\n删除边 1-3 后,图为");
graph.print();
graph.Print();
/* 添加顶点 */
Vertex v5 = new Vertex(6);
graph.addVertex(v5);
Vertex v5 = new(6);
graph.AddVertex(v5);
Console.WriteLine("\n添加顶点 6 后,图为");
graph.print();
graph.Print();
/* 删除顶点 */
// 顶点 3 即 v[1]
graph.removeVertex(v[1]);
graph.RemoveVertex(v[1]);
Console.WriteLine("\n删除顶点 3 后,图为");
graph.print();
graph.Print();
}
}

View File

@@ -8,8 +8,8 @@ namespace hello_algo.chapter_graph;
/* 基于邻接矩阵实现的无向图类 */
class GraphAdjMat {
List<int> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
List<List<int>> adjMat; // 邻接矩阵,行列索引对应“顶点索引”
readonly List<int> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
readonly List<List<int>> adjMat; // 邻接矩阵,行列索引对应“顶点索引”
/* 构造函数 */
public GraphAdjMat(int[] vertices, int[][] edges) {
@@ -17,27 +17,27 @@ class GraphAdjMat {
this.adjMat = new List<List<int>>();
// 添加顶点
foreach (int val in vertices) {
addVertex(val);
AddVertex(val);
}
// 添加边
// 请注意edges 元素代表顶点索引,即对应 vertices 元素索引
foreach (int[] e in edges) {
addEdge(e[0], e[1]);
AddEdge(e[0], e[1]);
}
}
/* 获取顶点数量 */
public int size() {
public int Size() {
return vertices.Count;
}
/* 添加顶点 */
public void addVertex(int val) {
int n = size();
public void AddVertex(int val) {
int n = Size();
// 向顶点列表中添加新顶点的值
vertices.Add(val);
// 在邻接矩阵中添加一行
List<int> newRow = new List<int>(n);
List<int> newRow = new(n);
for (int j = 0; j < n; j++) {
newRow.Add(0);
}
@@ -49,8 +49,8 @@ class GraphAdjMat {
}
/* 删除顶点 */
public void removeVertex(int index) {
if (index >= size())
public void RemoveVertex(int index) {
if (index >= Size())
throw new IndexOutOfRangeException();
// 在顶点列表中移除索引 index 的顶点
vertices.RemoveAt(index);
@@ -64,9 +64,9 @@ class GraphAdjMat {
/* 添加边 */
// 参数 i, j 对应 vertices 元素索引
public void addEdge(int i, int j) {
public void AddEdge(int i, int j) {
// 索引越界与相等处理
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)
throw new IndexOutOfRangeException();
// 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i)
adjMat[i][j] = 1;
@@ -75,16 +75,16 @@ class GraphAdjMat {
/* 删除边 */
// 参数 i, j 对应 vertices 元素索引
public void removeEdge(int i, int j) {
public void RemoveEdge(int i, int j) {
// 索引越界与相等处理
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)
throw new IndexOutOfRangeException();
adjMat[i][j] = 0;
adjMat[j][i] = 0;
}
/* 打印邻接矩阵 */
public void print() {
public void Print() {
Console.Write("顶点列表 = ");
PrintUtil.PrintList(vertices);
Console.WriteLine("邻接矩阵 =");
@@ -101,31 +101,31 @@ public class graph_adjacency_matrix {
int[][] edges = new int[][] { new int[] { 0, 1 }, new int[] { 0, 3 },
new int[] { 1, 2 }, new int[] { 2, 3 },
new int[] { 2, 4 }, new int[] { 3, 4 } };
GraphAdjMat graph = new GraphAdjMat(vertices, edges);
GraphAdjMat graph = new(vertices, edges);
Console.WriteLine("\n初始化后图为");
graph.print();
graph.Print();
/* 添加边 */
// 顶点 1, 2 的索引分别为 0, 2
graph.addEdge(0, 2);
graph.AddEdge(0, 2);
Console.WriteLine("\n添加边 1-2 后,图为");
graph.print();
graph.Print();
/* 删除边 */
// 顶点 1, 3 的索引分别为 0, 1
graph.removeEdge(0, 1);
graph.RemoveEdge(0, 1);
Console.WriteLine("\n删除边 1-3 后,图为");
graph.print();
graph.Print();
/* 添加顶点 */
graph.addVertex(6);
graph.AddVertex(6);
Console.WriteLine("\n添加顶点 6 后,图为");
graph.print();
graph.Print();
/* 删除顶点 */
// 顶点 3 的索引为 1
graph.removeVertex(1);
graph.RemoveVertex(1);
Console.WriteLine("\n删除顶点 3 后,图为");
graph.print();
graph.Print();
}
}

View File

@@ -9,13 +9,13 @@ namespace hello_algo.chapter_graph;
public class graph_bfs {
/* 广度优先遍历 BFS */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
public static List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
public static List<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {
// 顶点遍历序列
List<Vertex> res = new List<Vertex>();
List<Vertex> res = new();
// 哈希表,用于记录已被访问过的顶点
HashSet<Vertex> visited = new HashSet<Vertex>() { startVet };
HashSet<Vertex> visited = new() { startVet };
// 队列用于实现 BFS
Queue<Vertex> que = new Queue<Vertex>();
Queue<Vertex> que = new();
que.Enqueue(startVet);
// 以顶点 vet 为起点,循环直至访问完所有顶点
while (que.Count > 0) {
@@ -46,12 +46,12 @@ public class graph_bfs {
new Vertex[2] { v[5], v[8] }, new Vertex[2] { v[6], v[7] }, new Vertex[2] { v[7], v[8] }
};
GraphAdjList graph = new GraphAdjList(edges);
GraphAdjList graph = new(edges);
Console.WriteLine("\n初始化后图为");
graph.print();
graph.Print();
/* 广度优先遍历 BFS */
List<Vertex> res = graphBFS(graph, v[0]);
List<Vertex> res = GraphBFS(graph, v[0]);
Console.WriteLine("\n广度优先遍历BFS顶点序列为");
Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res)));
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_graph;
public class graph_dfs {
/* 深度优先遍历 DFS 辅助函数 */
public void dfs(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) {
public void Dfs(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) {
res.Add(vet); // 记录访问顶点
visited.Add(vet); // 标记该顶点已被访问
// 遍历该顶点的所有邻接顶点
@@ -17,18 +17,18 @@ public class graph_dfs {
continue; // 跳过已被访问过的顶点
}
// 递归访问邻接顶点
dfs(graph, visited, res, adjVet);
Dfs(graph, visited, res, adjVet);
}
}
/* 深度优先遍历 DFS */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
public List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
public List<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {
// 顶点遍历序列
List<Vertex> res = new List<Vertex>();
List<Vertex> res = new();
// 哈希表,用于记录已被访问过的顶点
HashSet<Vertex> visited = new HashSet<Vertex>();
dfs(graph, visited, res, startVet);
HashSet<Vertex> visited = new();
Dfs(graph, visited, res, startVet);
return res;
}
@@ -42,12 +42,12 @@ public class graph_dfs {
new Vertex[2] { v[2], v[5] }, new Vertex[2] { v[4], v[5] }, new Vertex[2] { v[5], v[6] },
};
GraphAdjList graph = new GraphAdjList(edges);
GraphAdjList graph = new(edges);
Console.WriteLine("\n初始化后图为");
graph.print();
graph.Print();
/* 深度优先遍历 DFS */
List<Vertex> res = graphDFS(graph, v[0]);
List<Vertex> res = GraphDFS(graph, v[0]);
Console.WriteLine("\n深度优先遍历DFS顶点序列为");
Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res)));
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_greedy;
public class coin_change_greedy {
/* 零钱兑换:贪心 */
public int coinChangeGreedy(int[] coins, int amt) {
public int CoinChangeGreedy(int[] coins, int amt) {
// 假设 coins 列表有序
int i = coins.Length - 1;
int count = 0;
@@ -31,14 +31,14 @@ public class coin_change_greedy {
// 贪心:能够保证找到全局最优解
int[] coins = { 1, 5, 10, 20, 50, 100 };
int amt = 186;
int res = coinChangeGreedy(coins, amt);
int res = CoinChangeGreedy(coins, amt);
Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt);
Console.WriteLine("凑到 " + amt + " 所需的最少硬币数量为 " + res);
// 贪心:无法保证找到全局最优解
coins = new int[] { 1, 20, 50 };
amt = 60;
res = coinChangeGreedy(coins, amt);
res = CoinChangeGreedy(coins, amt);
Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt);
Console.WriteLine("凑到 " + amt + " 所需的最少硬币数量为 " + res);
Console.WriteLine("实际上需要的最少数量为 3 ,即 20 + 20 + 20");
@@ -46,7 +46,7 @@ public class coin_change_greedy {
// 贪心:无法保证找到全局最优解
coins = new int[] { 1, 49, 50 };
amt = 98;
res = coinChangeGreedy(coins, amt);
res = CoinChangeGreedy(coins, amt);
Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt);
Console.WriteLine("凑到 " + amt + " 所需的最少硬币数量为 " + res);
Console.WriteLine("实际上需要的最少数量为 2 ,即 49 + 49");

View File

@@ -19,7 +19,7 @@ class Item {
public class fractional_knapsack {
/* 分数背包:贪心 */
public double fractionalKnapsack(int[] wgt, int[] val, int cap) {
public double FractionalKnapsack(int[] wgt, int[] val, int cap) {
// 创建物品列表,包含两个属性:重量、价值
Item[] items = new Item[wgt.Length];
for (int i = 0; i < wgt.Length; i++) {
@@ -51,7 +51,7 @@ public class fractional_knapsack {
int cap = 50;
// 贪心算法
double res = fractionalKnapsack(wgt, val, cap);
double res = FractionalKnapsack(wgt, val, cap);
Console.WriteLine("不超过背包容量的最大物品价值为 " + res);
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_greedy;
public class max_capacity {
/* 最大容量:贪心 */
public int maxCapacity(int[] ht) {
public int MaxCapacity(int[] ht) {
// 初始化 i, j 分列数组两端
int i = 0, j = ht.Length - 1;
// 初始最大容量为 0
@@ -33,7 +33,7 @@ public class max_capacity {
int[] ht = { 3, 8, 5, 2, 7, 7, 3, 4 };
// 贪心算法
int res = maxCapacity(ht);
int res = MaxCapacity(ht);
Console.WriteLine("最大容量为 " + res);
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_greedy;
public class max_product_cutting {
/* 最大切分乘积:贪心 */
public int maxProductCutting(int n) {
public int MaxProductCutting(int n) {
// 当 n <= 3 时,必须切分出一个 1
if (n <= 3) {
return 1 * (n - 1);
@@ -33,7 +33,7 @@ public class max_product_cutting {
int n = 58;
// 贪心算法
int res = maxProductCutting(n);
int res = MaxProductCutting(n);
Console.WriteLine("最大切分乘积为" + res);
}
}

View File

@@ -18,7 +18,7 @@ class Pair {
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
private List<Pair?> buckets;
private readonly List<Pair?> buckets;
public ArrayHashMap() {
// 初始化数组,包含 100 个桶
buckets = new();
@@ -28,35 +28,35 @@ class ArrayHashMap {
}
/* 哈希函数 */
private int hashFunc(int key) {
private int HashFunc(int key) {
int index = key % 100;
return index;
}
/* 查询操作 */
public string? get(int key) {
int index = hashFunc(key);
public string? Get(int key) {
int index = HashFunc(key);
Pair? pair = buckets[index];
if (pair == null) return null;
return pair.val;
}
/* 添加操作 */
public void put(int key, string val) {
Pair pair = new Pair(key, val);
int index = hashFunc(key);
public void Put(int key, string val) {
Pair pair = new(key, val);
int index = HashFunc(key);
buckets[index] = pair;
}
/* 删除操作 */
public void remove(int key) {
int index = hashFunc(key);
public void Remove(int key) {
int index = HashFunc(key);
// 置为 null ,代表删除
buckets[index] = null;
}
/* 获取所有键值对 */
public List<Pair> pairSet() {
public List<Pair> PairSet() {
List<Pair> pairSet = new();
foreach (Pair? pair in buckets) {
if (pair != null)
@@ -66,7 +66,7 @@ class ArrayHashMap {
}
/* 获取所有键 */
public List<int> keySet() {
public List<int> KeySet() {
List<int> keySet = new();
foreach (Pair? pair in buckets) {
if (pair != null)
@@ -76,7 +76,7 @@ class ArrayHashMap {
}
/* 获取所有值 */
public List<string> valueSet() {
public List<string> ValueSet() {
List<string> valueSet = new();
foreach (Pair? pair in buckets) {
if (pair != null)
@@ -86,8 +86,8 @@ class ArrayHashMap {
}
/* 打印哈希表 */
public void print() {
foreach (Pair kv in pairSet()) {
public void Print() {
foreach (Pair kv in PairSet()) {
Console.WriteLine(kv.key + " -> " + kv.val);
}
}
@@ -98,40 +98,40 @@ public class array_hash_map {
[Test]
public void Test() {
/* 初始化哈希表 */
ArrayHashMap map = new ArrayHashMap();
ArrayHashMap map = new();
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.put(12836, "小哈");
map.put(15937, "小啰");
map.put(16750, "小算");
map.put(13276, "小法");
map.put(10583, "小鸭");
map.Put(12836, "小哈");
map.Put(15937, "小啰");
map.Put(16750, "小算");
map.Put(13276, "小法");
map.Put(10583, "小鸭");
Console.WriteLine("\n添加完成后哈希表为\nKey -> Value");
map.print();
map.Print();
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
string? name = map.get(15937);
string? name = map.Get(15937);
Console.WriteLine("\n输入学号 15937 ,查询到姓名 " + name);
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
map.remove(10583);
map.Remove(10583);
Console.WriteLine("\n删除 10583 后,哈希表为\nKey -> Value");
map.print();
map.Print();
/* 遍历哈希表 */
Console.WriteLine("\n遍历键值对 Key->Value");
foreach (Pair kv in map.pairSet()) {
foreach (Pair kv in map.PairSet()) {
Console.WriteLine(kv.key + " -> " + kv.val);
}
Console.WriteLine("\n单独遍历键 Key");
foreach (int key in map.keySet()) {
foreach (int key in map.KeySet()) {
Console.WriteLine(key);
}
Console.WriteLine("\n单独遍历值 Value");
foreach (string val in map.valueSet()) {
foreach (string val in map.ValueSet()) {
Console.WriteLine(val);
}
}

View File

@@ -29,7 +29,7 @@ public class built_in_hash {
int hashTup = arr.GetHashCode();
Console.WriteLine("数组 [" + string.Join(", ", arr) + "] 的哈希值为 " + hashTup);
ListNode obj = new ListNode(0);
ListNode obj = new(0);
int hashObj = obj.GetHashCode();
Console.WriteLine("节点对象 " + obj + " 的哈希值为 " + hashObj);
}

View File

@@ -11,15 +11,15 @@ public class hash_map {
[Test]
public void Test() {
/* 初始化哈希表 */
Dictionary<int, string> map = new();
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.Add(12836, "小");
map.Add(15937, "小");
map.Add(16750, "小");
map.Add(13276, "小");
map.Add(10583, "小鸭");
Dictionary<int, string> map = new() {
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
{ 12836, "小哈" },
{ 15937, "小" },
{ 16750, "小" },
{ 13276, "小" },
{ 10583, "小" }
};
Console.WriteLine("\n添加完成后哈希表为\nKey -> Value");
PrintUtil.PrintHashMap(map);

View File

@@ -10,8 +10,8 @@ namespace hello_algo.chapter_hashing;
class HashMapChaining {
int size; // 键值对数量
int capacity; // 哈希表容量
double loadThres; // 触发扩容的负载因子阈值
int extendRatio; // 扩容倍数
readonly double loadThres; // 触发扩容的负载因子阈值
readonly int extendRatio; // 扩容倍数
List<List<Pair>> buckets; // 桶数组
/* 构造方法 */
@@ -27,18 +27,18 @@ class HashMapChaining {
}
/* 哈希函数 */
private int hashFunc(int key) {
private int HashFunc(int key) {
return key % capacity;
}
/* 负载因子 */
private double loadFactor() {
private double LoadFactor() {
return (double)size / capacity;
}
/* 查询操作 */
public string? get(int key) {
int index = hashFunc(key);
public string? Get(int key) {
int index = HashFunc(key);
// 遍历桶,若找到 key 则返回对应 val
foreach (Pair pair in buckets[index]) {
if (pair.key == key) {
@@ -50,12 +50,12 @@ class HashMapChaining {
}
/* 添加操作 */
public void put(int key, string val) {
public void Put(int key, string val) {
// 当负载因子超过阈值时,执行扩容
if (loadFactor() > loadThres) {
extend();
if (LoadFactor() > loadThres) {
Extend();
}
int index = hashFunc(key);
int index = HashFunc(key);
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回
foreach (Pair pair in buckets[index]) {
if (pair.key == key) {
@@ -69,8 +69,8 @@ class HashMapChaining {
}
/* 删除操作 */
public void remove(int key) {
int index = hashFunc(key);
public void Remove(int key) {
int index = HashFunc(key);
// 遍历桶,从中删除键值对
foreach (Pair pair in buckets[index].ToList()) {
if (pair.key == key) {
@@ -82,7 +82,7 @@ class HashMapChaining {
}
/* 扩容哈希表 */
private void extend() {
private void Extend() {
// 暂存原哈希表
List<List<Pair>> bucketsTmp = buckets;
// 初始化扩容后的新哈希表
@@ -95,15 +95,15 @@ class HashMapChaining {
// 将键值对从原哈希表搬运至新哈希表
foreach (List<Pair> bucket in bucketsTmp) {
foreach (Pair pair in bucket) {
put(pair.key, pair.val);
Put(pair.key, pair.val);
}
}
}
/* 打印哈希表 */
public void print() {
public void Print() {
foreach (List<Pair> bucket in buckets) {
List<string> res = new List<string>();
List<string> res = new();
foreach (Pair pair in bucket) {
res.Add(pair.key + " -> " + pair.val);
}
@@ -118,27 +118,27 @@ public class hash_map_chaining {
[Test]
public void Test() {
/* 初始化哈希表 */
HashMapChaining map = new HashMapChaining();
HashMapChaining map = new();
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.put(12836, "小哈");
map.put(15937, "小啰");
map.put(16750, "小算");
map.put(13276, "小法");
map.put(10583, "小鸭");
map.Put(12836, "小哈");
map.Put(15937, "小啰");
map.Put(16750, "小算");
map.Put(13276, "小法");
map.Put(10583, "小鸭");
Console.WriteLine("\n添加完成后哈希表为\nKey -> Value");
map.print();
map.Print();
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
string name = map.get(13276);
string? name = map.Get(13276);
Console.WriteLine("\n输入学号 13276 ,查询到姓名 " + name);
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
map.remove(12836);
map.Remove(12836);
Console.WriteLine("\n删除 12836 后,哈希表为\nKey -> Value");
map.print();
map.Print();
}
}

View File

@@ -10,10 +10,10 @@ namespace hello_algo.chapter_hashing;
class HashMapOpenAddressing {
private int size; // 键值对数量
private int capacity = 4; // 哈希表容量
private double loadThres = 2.0 / 3.0; // 触发扩容的负载因子阈值
private int extendRatio = 2; // 扩容倍数
private readonly double loadThres = 2.0 / 3.0; // 触发扩容的负载因子阈值
private readonly int extendRatio = 2; // 扩容倍数
private Pair[] buckets; // 桶数组
private Pair TOMBSTONE = new Pair(-1, "-1"); // 删除标记
private readonly Pair TOMBSTONE = new(-1, "-1"); // 删除标记
/* 构造方法 */
public HashMapOpenAddressing() {
@@ -22,18 +22,18 @@ class HashMapOpenAddressing {
}
/* 哈希函数 */
private int hashFunc(int key) {
private int HashFunc(int key) {
return key % capacity;
}
/* 负载因子 */
private double loadFactor() {
private double LoadFactor() {
return (double)size / capacity;
}
/* 搜索 key 对应的桶索引 */
private int findBucket(int key) {
int index = hashFunc(key);
private int FindBucket(int key) {
int index = HashFunc(key);
int firstTombstone = -1;
// 线性探测,当遇到空桶时跳出
while (buckets[index] != null) {
@@ -59,9 +59,9 @@ class HashMapOpenAddressing {
}
/* 查询操作 */
public string? get(int key) {
public string? Get(int key) {
// 搜索 key 对应的桶索引
int index = findBucket(key);
int index = FindBucket(key);
// 若找到键值对,则返回对应 val
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
return buckets[index].val;
@@ -71,13 +71,13 @@ class HashMapOpenAddressing {
}
/* 添加操作 */
public void put(int key, string val) {
public void Put(int key, string val) {
// 当负载因子超过阈值时,执行扩容
if (loadFactor() > loadThres) {
extend();
if (LoadFactor() > loadThres) {
Extend();
}
// 搜索 key 对应的桶索引
int index = findBucket(key);
int index = FindBucket(key);
// 若找到键值对,则覆盖 val 并返回
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
buckets[index].val = val;
@@ -89,9 +89,9 @@ class HashMapOpenAddressing {
}
/* 删除操作 */
public void remove(int key) {
public void Remove(int key) {
// 搜索 key 对应的桶索引
int index = findBucket(key);
int index = FindBucket(key);
// 若找到键值对,则用删除标记覆盖它
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
buckets[index] = TOMBSTONE;
@@ -100,7 +100,7 @@ class HashMapOpenAddressing {
}
/* 扩容哈希表 */
private void extend() {
private void Extend() {
// 暂存原哈希表
Pair[] bucketsTmp = buckets;
// 初始化扩容后的新哈希表
@@ -110,13 +110,13 @@ class HashMapOpenAddressing {
// 将键值对从原哈希表搬运至新哈希表
foreach (Pair pair in bucketsTmp) {
if (pair != null && pair != TOMBSTONE) {
put(pair.key, pair.val);
Put(pair.key, pair.val);
}
}
}
/* 打印哈希表 */
public void print() {
public void Print() {
foreach (Pair pair in buckets) {
if (pair == null) {
Console.WriteLine("null");
@@ -133,27 +133,27 @@ public class hash_map_open_addressing {
[Test]
public void Test() {
/* 初始化哈希表 */
HashMapOpenAddressing map = new HashMapOpenAddressing();
HashMapOpenAddressing map = new();
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.put(12836, "小哈");
map.put(15937, "小啰");
map.put(16750, "小算");
map.put(13276, "小法");
map.put(10583, "小鸭");
map.Put(12836, "小哈");
map.Put(15937, "小啰");
map.Put(16750, "小算");
map.Put(13276, "小法");
map.Put(10583, "小鸭");
Console.WriteLine("\n添加完成后哈希表为\nKey -> Value");
map.print();
map.Print();
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
string name = map.get(13276);
string? name = map.Get(13276);
Console.WriteLine("\n输入学号 13276 ,查询到姓名 " + name);
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
map.remove(16750);
map.Remove(16750);
Console.WriteLine("\n删除 16750 后,哈希表为\nKey -> Value");
map.print();
map.Print();
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_hashing;
public class simple_hash {
/* 加法哈希 */
public static int addHash(string key) {
public static int AddHash(string key) {
long hash = 0;
const int MODULUS = 1000000007;
foreach (char c in key) {
@@ -18,7 +18,7 @@ public class simple_hash {
}
/* 乘法哈希 */
public static int mulHash(string key) {
public static int MulHash(string key) {
long hash = 0;
const int MODULUS = 1000000007;
foreach (char c in key) {
@@ -28,7 +28,7 @@ public class simple_hash {
}
/* 异或哈希 */
public static int xorHash(string key) {
public static int XorHash(string key) {
int hash = 0;
const int MODULUS = 1000000007;
foreach (char c in key) {
@@ -38,7 +38,7 @@ public class simple_hash {
}
/* 旋转哈希 */
public static int rotHash(string key) {
public static int RotHash(string key) {
long hash = 0;
const int MODULUS = 1000000007;
foreach (char c in key) {
@@ -51,16 +51,16 @@ public class simple_hash {
public void Test() {
string key = "Hello 算法";
int hash = addHash(key);
int hash = AddHash(key);
Console.WriteLine("加法哈希值为 " + hash);
hash = mulHash(key);
hash = MulHash(key);
Console.WriteLine("乘法哈希值为 " + hash);
hash = xorHash(key);
hash = XorHash(key);
Console.WriteLine("异或哈希值为 " + hash);
hash = rotHash(key);
hash = RotHash(key);
Console.WriteLine("旋转哈希值为 " + hash);
}
}

View File

@@ -7,32 +7,33 @@
namespace hello_algo.chapter_heap;
public class heap {
public void testPush(PriorityQueue<int, int> heap, int val) {
public void TestPush(PriorityQueue<int, int> heap, int val) {
heap.Enqueue(val, val); // 元素入堆
Console.WriteLine($"\n元素 {val} 入堆后\n");
PrintUtil.PrintHeap(heap);
}
public void testPop(PriorityQueue<int, int> heap) {
public void TestPop(PriorityQueue<int, int> heap) {
int val = heap.Dequeue(); // 堆顶元素出堆
Console.WriteLine($"\n堆顶元素 {val} 出堆后\n");
PrintUtil.PrintHeap(heap);
}
[Test]
public void Test() {
/* 初始化堆 */
// 初始化小顶堆
PriorityQueue<int, int> minHeap = new PriorityQueue<int, int>();
PriorityQueue<int, int> minHeap = new();
// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
PriorityQueue<int, int> maxHeap = new PriorityQueue<int, int>(Comparer<int>.Create((x, y) => y - x));
PriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y - x));
Console.WriteLine("以下测试样例为大顶堆");
/* 元素入堆 */
testPush(maxHeap, 1);
testPush(maxHeap, 3);
testPush(maxHeap, 2);
testPush(maxHeap, 5);
testPush(maxHeap, 4);
TestPush(maxHeap, 1);
TestPush(maxHeap, 3);
TestPush(maxHeap, 2);
TestPush(maxHeap, 5);
TestPush(maxHeap, 4);
/* 获取堆顶元素 */
int peek = maxHeap.Peek();
@@ -40,11 +41,11 @@ public class heap {
/* 堆顶元素出堆 */
// 出堆元素会形成一个从大到小的序列
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
TestPop(maxHeap);
TestPop(maxHeap);
TestPop(maxHeap);
TestPop(maxHeap);
TestPop(maxHeap);
/* 获取堆大小 */
int size = maxHeap.Count;

View File

@@ -21,106 +21,106 @@ class MaxHeap {
// 将列表元素原封不动添加进堆
maxHeap = new List<int>(nums);
// 堆化除叶节点以外的其他所有节点
var size = parent(this.size() - 1);
var size = Parent(this.Size() - 1);
for (int i = size; i >= 0; i--) {
siftDown(i);
SiftDown(i);
}
}
/* 获取左子节点索引 */
int left(int i) {
int Left(int i) {
return 2 * i + 1;
}
/* 获取右子节点索引 */
int right(int i) {
int Right(int i) {
return 2 * i + 2;
}
/* 获取父节点索引 */
int parent(int i) {
int Parent(int i) {
return (i - 1) / 2; // 向下整除
}
/* 访问堆顶元素 */
public int peek() {
public int Peek() {
return maxHeap[0];
}
/* 元素入堆 */
public void push(int val) {
public void Push(int val) {
// 添加节点
maxHeap.Add(val);
// 从底至顶堆化
siftUp(size() - 1);
SiftUp(Size() - 1);
}
/* 获取堆大小 */
public int size() {
public int Size() {
return maxHeap.Count;
}
/* 判断堆是否为空 */
public bool isEmpty() {
return size() == 0;
public bool IsEmpty() {
return Size() == 0;
}
/* 从节点 i 开始,从底至顶堆化 */
void siftUp(int i) {
void SiftUp(int i) {
while (true) {
// 获取节点 i 的父节点
int p = parent(i);
int p = Parent(i);
// 若“越过根节点”或“节点无须修复”,则结束堆化
if (p < 0 || maxHeap[i] <= maxHeap[p])
break;
// 交换两节点
swap(i, p);
Swap(i, p);
// 循环向上堆化
i = p;
}
}
/* 元素出堆 */
public int pop() {
public int Pop() {
// 判空处理
if (isEmpty())
if (IsEmpty())
throw new IndexOutOfRangeException();
// 交换根节点与最右叶节点(即交换首元素与尾元素)
swap(0, size() - 1);
Swap(0, Size() - 1);
// 删除节点
int val = maxHeap.Last();
maxHeap.RemoveAt(size() - 1);
maxHeap.RemoveAt(Size() - 1);
// 从顶至底堆化
siftDown(0);
SiftDown(0);
// 返回堆顶元素
return val;
}
/* 从节点 i 开始,从顶至底堆化 */
void siftDown(int i) {
void SiftDown(int i) {
while (true) {
// 判断节点 i, l, r 中值最大的节点,记为 ma
int l = left(i), r = right(i), ma = i;
if (l < size() && maxHeap[l] > maxHeap[ma])
int l = Left(i), r = Right(i), ma = i;
if (l < Size() && maxHeap[l] > maxHeap[ma])
ma = l;
if (r < size() && maxHeap[r] > maxHeap[ma])
if (r < Size() && maxHeap[r] > maxHeap[ma])
ma = r;
// 若“节点 i 最大”或“越过叶节点”,则结束堆化
if (ma == i) break;
// 交换两节点
swap(i, ma);
Swap(i, ma);
// 循环向下堆化
i = ma;
}
}
/* 交换元素 */
void swap(int i, int p) {
void Swap(int i, int p) {
(maxHeap[i], maxHeap[p]) = (maxHeap[p], maxHeap[i]);
}
/* 打印堆(二叉树) */
public void print() {
public void Print() {
var queue = new Queue<int>(maxHeap);
PrintUtil.PrintHeap(queue);
}
@@ -130,31 +130,31 @@ public class my_heap {
[Test]
public void Test() {
/* 初始化大顶堆 */
MaxHeap maxHeap = new MaxHeap(new int[] { 9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2 });
MaxHeap maxHeap = new(new int[] { 9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2 });
Console.WriteLine("\n输入列表并建堆后");
maxHeap.print();
maxHeap.Print();
/* 获取堆顶元素 */
int peek = maxHeap.peek();
int peek = maxHeap.Peek();
Console.WriteLine($"堆顶元素为 {peek}");
/* 元素入堆 */
int val = 7;
maxHeap.push(val);
maxHeap.Push(val);
Console.WriteLine($"元素 {val} 入堆后");
maxHeap.print();
maxHeap.Print();
/* 堆顶元素出堆 */
peek = maxHeap.pop();
peek = maxHeap.Pop();
Console.WriteLine($"堆顶元素 {peek} 出堆后");
maxHeap.print();
maxHeap.Print();
/* 获取堆大小 */
int size = maxHeap.size();
int size = maxHeap.Size();
Console.WriteLine($"堆元素数量为 {size}");
/* 判断堆是否为空 */
bool isEmpty = maxHeap.isEmpty();
bool isEmpty = maxHeap.IsEmpty();
Console.WriteLine($"堆是否为空 {isEmpty}");
}
}

View File

@@ -8,8 +8,8 @@ namespace hello_algo.chapter_heap;
public class top_k {
/* 基于堆查找数组中最大的 k 个元素 */
public static PriorityQueue<int, int> topKHeap(int[] nums, int k) {
PriorityQueue<int, int> heap = new PriorityQueue<int, int>();
public static PriorityQueue<int, int> TopKHeap(int[] nums, int k) {
PriorityQueue<int, int> heap = new();
// 将数组的前 k 个元素入堆
for (int i = 0; i < k; i++) {
heap.Enqueue(nums[i], nums[i]);
@@ -29,7 +29,7 @@ public class top_k {
public void Test() {
int[] nums = { 1, 7, 6, 3, 2 };
int k = 3;
PriorityQueue<int, int> res = topKHeap(nums, k);
PriorityQueue<int, int> res = TopKHeap(nums, k);
Console.WriteLine("最大的 " + k + " 个元素为");
PrintUtil.PrintHeap(res);
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_searching;
public class binary_search {
/* 二分查找(双闭区间) */
static int binarySearch(int[] nums, int target) {
static int BinarySearch(int[] nums, int target) {
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
int i = 0, j = nums.Length - 1;
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
@@ -26,7 +26,7 @@ public class binary_search {
}
/* 二分查找(左闭右开) */
static int binarySearchLCRO(int[] nums, int target) {
static int BinarySearchLCRO(int[] nums, int target) {
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
int i = 0, j = nums.Length;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
@@ -49,11 +49,11 @@ public class binary_search {
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
/* 二分查找(双闭区间) */
int index = binarySearch(nums, target);
int index = BinarySearch(nums, target);
Console.WriteLine("目标元素 6 的索引 = " + index);
/* 二分查找(左闭右开) */
index = binarySearchLCRO(nums, target);
index = BinarySearchLCRO(nums, target);
Console.WriteLine("目标元素 6 的索引 = " + index);
}
}

View File

@@ -8,9 +8,9 @@ namespace hello_algo.chapter_searching;
public class binary_search_edge {
/* 二分查找最左一个 target */
public int binarySearchLeftEdge(int[] nums, int target) {
public int BinarySearchLeftEdge(int[] nums, int target) {
// 等价于查找 target 的插入点
int i = binary_search_insertion.binarySearchInsertion(nums, target);
int i = binary_search_insertion.BinarySearchInsertion(nums, target);
// 未找到 target ,返回 -1
if (i == nums.Length || nums[i] != target) {
return -1;
@@ -22,7 +22,7 @@ public class binary_search_edge {
/* 二分查找最右一个 target */
public int binarySearchRightEdge(int[] nums, int target) {
// 转化为查找最左一个 target + 1
int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);
int i = binary_search_insertion.BinarySearchInsertion(nums, target + 1);
// j 指向最右一个 target i 指向首个大于 target 的元素
int j = i - 1;
// 未找到 target ,返回 -1
@@ -41,7 +41,7 @@ public class binary_search_edge {
// 二分查找左边界和右边界
foreach (int target in new int[] { 6, 7 }) {
int index = binarySearchLeftEdge(nums, target);
int index = BinarySearchLeftEdge(nums, target);
Console.WriteLine("最左一个元素 " + target + " 的索引为 " + index);
index = binarySearchRightEdge(nums, target);
Console.WriteLine("最右一个元素 " + target + " 的索引为 " + index);

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_searching;
public class binary_search_insertion {
/* 二分查找插入点(无重复元素) */
public int binarySearchInsertionSimple(int[] nums, int target) {
public int BinarySearchInsertionSimple(int[] nums, int target) {
int i = 0, j = nums.Length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m
@@ -25,7 +25,7 @@ public class binary_search_insertion {
}
/* 二分查找插入点(存在重复元素) */
public static int binarySearchInsertion(int[] nums, int target) {
public static int BinarySearchInsertion(int[] nums, int target) {
int i = 0, j = nums.Length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m
@@ -48,7 +48,7 @@ public class binary_search_insertion {
Console.WriteLine("\n数组 nums = " + nums.PrintList());
// 二分查找插入点
foreach (int target in new int[] { 6, 9 }) {
int index = binarySearchInsertionSimple(nums, target);
int index = BinarySearchInsertionSimple(nums, target);
Console.WriteLine("元素 " + target + " 的插入点的索引为 " + index);
}
@@ -57,7 +57,7 @@ public class binary_search_insertion {
Console.WriteLine("\n数组 nums = " + nums.PrintList());
// 二分查找插入点
foreach (int target in new int[] { 2, 6, 20 }) {
int index = binarySearchInsertion(nums, target);
int index = BinarySearchInsertion(nums, target);
Console.WriteLine("元素 " + target + " 的插入点的索引为 " + index);
}
}

View File

@@ -8,14 +8,14 @@ namespace hello_algo.chapter_searching;
public class hashing_search {
/* 哈希查找(数组) */
static int hashingSearchArray(Dictionary<int, int> map, int target) {
static int HashingSearchArray(Dictionary<int, int> map, int target) {
// 哈希表的 key: 目标元素value: 索引
// 若哈希表中无此 key ,返回 -1
return map.GetValueOrDefault(target, -1);
}
/* 哈希查找(链表) */
static ListNode? hashingSearchLinkedList(Dictionary<int, ListNode> map, int target) {
static ListNode? HashingSearchLinkedList(Dictionary<int, ListNode> map, int target) {
// 哈希表的 key: 目标节点值value: 节点对象
// 若哈希表中无此 key ,返回 null
@@ -33,7 +33,7 @@ public class hashing_search {
for (int i = 0; i < nums.Length; i++) {
map[nums[i]] = i; // key: 元素value: 索引
}
int index = hashingSearchArray(map, target);
int index = HashingSearchArray(map, target);
Console.WriteLine("目标元素 3 的索引 = " + index);
/* 哈希查找(链表) */
@@ -44,7 +44,7 @@ public class hashing_search {
map1[head.val] = head; // key: 节点值value: 节点
head = head.next;
}
ListNode? node = hashingSearchLinkedList(map1, target);
ListNode? node = HashingSearchLinkedList(map1, target);
Console.WriteLine("目标节点值 3 的对应节点对象为 " + node);
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_searching;
public class linear_search {
/* 线性查找(数组) */
static int linearSearchArray(int[] nums, int target) {
static int LinearSearchArray(int[] nums, int target) {
// 遍历数组
for (int i = 0; i < nums.Length; i++) {
// 找到目标元素,返回其索引
@@ -20,7 +20,7 @@ public class linear_search {
}
/* 线性查找(链表) */
static ListNode? linearSearchLinkedList(ListNode head, int target) {
static ListNode? LinearSearchLinkedList(ListNode? head, int target) {
// 遍历链表
while (head != null) {
// 找到目标节点,返回之
@@ -38,12 +38,12 @@ public class linear_search {
/* 在数组中执行线性查找 */
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);
/* 在链表中执行线性查找 */
ListNode head = ListNode.ArrToLinkedList(nums);
ListNode? node = linearSearchLinkedList(head, target);
ListNode? head = ListNode.ArrToLinkedList(nums);
ListNode? node = LinearSearchLinkedList(head, target);
Console.WriteLine("目标节点值 3 的对应节点对象为 " + node);
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_searching;
public class two_sum {
/* 方法一:暴力枚举 */
public static int[] twoSumBruteForce(int[] nums, int target) {
public static int[] TwoSumBruteForce(int[] nums, int target) {
int size = nums.Length;
// 两层循环,时间复杂度 O(n^2)
for (int i = 0; i < size - 1; i++) {
@@ -21,7 +21,7 @@ public class two_sum {
}
/* 方法二:辅助哈希表 */
public static int[] twoSumHashTable(int[] nums, int target) {
public static int[] TwoSumHashTable(int[] nums, int target) {
int size = nums.Length;
// 辅助哈希表,空间复杂度 O(n)
Dictionary<int, int> dic = new();
@@ -43,10 +43,10 @@ public class two_sum {
// ====== Driver Code ======
// 方法一
int[] res = twoSumBruteForce(nums, target);
int[] res = TwoSumBruteForce(nums, target);
Console.WriteLine("方法一 res = " + string.Join(",", res));
// 方法二
res = twoSumHashTable(nums, target);
res = TwoSumHashTable(nums, target);
Console.WriteLine("方法二 res = " + string.Join(",", res));
}
}

View File

@@ -8,23 +8,21 @@ namespace hello_algo.chapter_sorting;
public class bubble_sort {
/* 冒泡排序 */
static void bubbleSort(int[] nums) {
static void BubbleSort(int[] nums) {
// 外循环:未排序区间为 [0, i]
for (int i = nums.Length - 1; i > 0; i--) {
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// 交换 nums[j] 与 nums[j + 1]
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);
}
}
}
}
/* 冒泡排序(标志优化)*/
static void bubbleSortWithFlag(int[] nums) {
static void BubbleSortWithFlag(int[] nums) {
// 外循环:未排序区间为 [0, i]
for (int i = nums.Length - 1; i > 0; i--) {
bool flag = false; // 初始化标志位
@@ -32,9 +30,7 @@ public class bubble_sort {
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// 交换 nums[j] 与 nums[j + 1]
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);
flag = true; // 记录交换元素
}
}
@@ -45,11 +41,11 @@ public class bubble_sort {
[Test]
public void Test() {
int[] nums = { 4, 1, 3, 1, 5, 2 };
bubbleSort(nums);
BubbleSort(nums);
Console.WriteLine("冒泡排序完成后 nums = " + string.Join(",", nums));
int[] nums1 = { 4, 1, 3, 1, 5, 2 };
bubbleSortWithFlag(nums1);
BubbleSortWithFlag(nums1);
Console.WriteLine("冒泡排序完成后 nums1 = " + string.Join(",", nums1));
}
}

View File

@@ -8,10 +8,10 @@ namespace hello_algo.chapter_sorting;
public class bucket_sort {
/* 桶排序 */
public static void bucketSort(float[] nums) {
public static void BucketSort(float[] nums) {
// 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
int k = nums.Length / 2;
List<List<float>> buckets = new List<List<float>>();
List<List<float>> buckets = new();
for (int i = 0; i < k; i++) {
buckets.Add(new List<float>());
}
@@ -40,7 +40,7 @@ public class bucket_sort {
public void Test() {
// 设输入数据为浮点数,范围为 [0, 1)
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));
}
}

View File

@@ -9,7 +9,7 @@ namespace hello_algo.chapter_sorting;
public class counting_sort {
/* 计数排序 */
// 简单实现,无法用于排序对象
public static void countingSortNaive(int[] nums) {
public static void CountingSortNaive(int[] nums) {
// 1. 统计数组最大元素 m
int m = 0;
foreach (int num in nums) {
@@ -32,7 +32,7 @@ public class counting_sort {
/* 计数排序 */
// 完整实现,可排序对象,并且是稳定排序
static void countingSort(int[] nums) {
static void CountingSort(int[] nums) {
// 1. 统计数组最大元素 m
int m = 0;
foreach (int num in nums) {
@@ -67,11 +67,11 @@ public class counting_sort {
[Test]
public void Test() {
int[] nums = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
countingSortNaive(nums);
CountingSortNaive(nums);
Console.WriteLine("计数排序(无法排序对象)完成后 nums = " + string.Join(" ", nums));
int[] nums1 = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
countingSort(nums1);
CountingSort(nums1);
Console.WriteLine("计数排序完成后 nums1 = " + string.Join(" ", nums));
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_sorting;
public class heap_sort {
/* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */
public static void siftDown(int[] nums, int n, int i) {
public static void SiftDown(int[] nums, int n, int i) {
while (true) {
// 判断节点 i, l, r 中值最大的节点,记为 ma
int l = 2 * i + 1;
@@ -29,24 +29,24 @@ public class heap_sort {
}
/* 堆排序 */
public static void heapSort(int[] nums) {
public static void HeapSort(int[] nums) {
// 建堆操作:堆化除叶节点以外的其他所有节点
for (int i = nums.Length / 2 - 1; i >= 0; i--) {
siftDown(nums, nums.Length, i);
SiftDown(nums, nums.Length, i);
}
// 从堆中提取最大元素,循环 n-1 轮
for (int i = nums.Length - 1; i > 0; i--) {
// 交换根节点与最右叶节点(即交换首元素与尾元素)
(nums[i], nums[0]) = (nums[0], nums[i]);
// 以根节点为起点,从顶至底进行堆化
siftDown(nums, i, 0);
SiftDown(nums, i, 0);
}
}
[Test]
public void Test() {
int[] nums = { 4, 1, 3, 1, 5, 2 };
heapSort(nums);
HeapSort(nums);
Console.WriteLine("堆排序完成后 nums = " + string.Join(" ", nums));
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_sorting;
public class insertion_sort {
/* 插入排序 */
static void insertionSort(int[] nums) {
static void InsertionSort(int[] nums) {
// 外循环:已排序元素数量为 1, 2, ..., n
for (int i = 1; i < nums.Length; i++) {
int bas = nums[i], j = i - 1;
@@ -24,7 +24,7 @@ public class insertion_sort {
[Test]
public void Test() {
int[] nums = { 4, 1, 3, 1, 5, 2 };
insertionSort(nums);
InsertionSort(nums);
Console.WriteLine("插入排序完成后 nums = " + string.Join(",", nums));
}
}

View File

@@ -10,7 +10,7 @@ public class merge_sort {
/* 合并左子数组和右子数组 */
// 左子数组区间 [left, mid]
// 右子数组区间 [mid + 1, right]
static void merge(int[] nums, int left, int mid, int right) {
static void Merge(int[] nums, int left, int mid, int right) {
// 初始化辅助数组
int[] tmp = nums[left..(right + 1)];
// 左子数组的起始索引和结束索引
@@ -34,22 +34,22 @@ public class merge_sort {
}
/* 归并排序 */
static void mergeSort(int[] nums, int left, int right) {
static void MergeSort(int[] nums, int left, int right) {
// 终止条件
if (left >= right) return; // 当子数组长度为 1 时终止递归
// 划分阶段
int mid = (left + right) / 2; // 计算中点
mergeSort(nums, left, mid); // 递归左子数组
mergeSort(nums, mid + 1, right); // 递归右子数组
MergeSort(nums, left, mid); // 递归左子数组
MergeSort(nums, mid + 1, right); // 递归右子数组
// 合并阶段
merge(nums, left, mid, right);
Merge(nums, left, mid, right);
}
[Test]
public void Test() {
/* 归并排序 */
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));
}
}

View File

@@ -6,16 +6,14 @@
namespace hello_algo.chapter_sorting;
class QuickSort {
class quickSort {
/* 元素交换 */
static void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
static void Swap(int[] nums, int i, int j) {
(nums[j], nums[i]) = (nums[i], nums[j]);
}
/* 哨兵划分 */
static int partition(int[] nums, int left, int right) {
static int Partition(int[] nums, int left, int right) {
// 以 nums[left] 作为基准数
int i = left, j = right;
while (i < j) {
@@ -23,36 +21,34 @@ class QuickSort {
j--; // 从右向左找首个小于基准数的元素
while (i < j && nums[i] <= nums[left])
i++; // 从左向右找首个大于基准数的元素
swap(nums, i, j); // 交换这两个元素
Swap(nums, i, j); // 交换这两个元素
}
swap(nums, i, left); // 将基准数交换至两子数组的分界线
Swap(nums, i, left); // 将基准数交换至两子数组的分界线
return i; // 返回基准数的索引
}
/* 快速排序 */
public static void quickSort(int[] nums, int left, int right) {
public static void QuickSort(int[] nums, int left, int right) {
// 子数组长度为 1 时终止递归
if (left >= right)
return;
// 哨兵划分
int pivot = partition(nums, left, right);
int pivot = Partition(nums, left, right);
// 递归左子数组、右子数组
quickSort(nums, left, pivot - 1);
quickSort(nums, pivot + 1, right);
QuickSort(nums, left, pivot - 1);
QuickSort(nums, pivot + 1, right);
}
}
/* 快速排序类(中位基准数优化) */
class QuickSortMedian {
/* 元素交换 */
static void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
static void Swap(int[] nums, int i, int j) {
(nums[j], nums[i]) = (nums[i], nums[j]);
}
/* 选取三个元素的中位数 */
static int medianThree(int[] nums, int left, int mid, int right) {
static int MedianThree(int[] nums, int left, int mid, int right) {
// 此处使用异或运算来简化代码
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))
@@ -64,11 +60,11 @@ class QuickSortMedian {
}
/* 哨兵划分(三数取中值) */
static int partition(int[] nums, int left, int right) {
static int Partition(int[] nums, int left, int right) {
// 选取三个候选元素的中位数
int med = medianThree(nums, left, (left + right) / 2, right);
int med = MedianThree(nums, left, (left + right) / 2, right);
// 将中位数交换至数组最左端
swap(nums, left, med);
Swap(nums, left, med);
// 以 nums[left] 作为基准数
int i = left, j = right;
while (i < j) {
@@ -76,36 +72,34 @@ class QuickSortMedian {
j--; // 从右向左找首个小于基准数的元素
while (i < j && nums[i] <= nums[left])
i++; // 从左向右找首个大于基准数的元素
swap(nums, i, j); // 交换这两个元素
Swap(nums, i, j); // 交换这两个元素
}
swap(nums, i, left); // 将基准数交换至两子数组的分界线
Swap(nums, i, left); // 将基准数交换至两子数组的分界线
return i; // 返回基准数的索引
}
/* 快速排序 */
public static void quickSort(int[] nums, int left, int right) {
public static void QuickSort(int[] nums, int left, int right) {
// 子数组长度为 1 时终止递归
if (left >= right)
return;
// 哨兵划分
int pivot = partition(nums, left, right);
int pivot = Partition(nums, left, right);
// 递归左子数组、右子数组
quickSort(nums, left, pivot - 1);
quickSort(nums, pivot + 1, right);
QuickSort(nums, left, pivot - 1);
QuickSort(nums, pivot + 1, right);
}
}
/* 快速排序类(尾递归优化) */
class QuickSortTailCall {
/* 元素交换 */
static void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
static void Swap(int[] nums, int i, int j) {
(nums[j], nums[i]) = (nums[i], nums[j]);
}
/* 哨兵划分 */
static int partition(int[] nums, int left, int right) {
static int Partition(int[] nums, int left, int right) {
// 以 nums[left] 作为基准数
int i = left, j = right;
while (i < j) {
@@ -113,24 +107,24 @@ class QuickSortTailCall {
j--; // 从右向左找首个小于基准数的元素
while (i < j && nums[i] <= nums[left])
i++; // 从左向右找首个大于基准数的元素
swap(nums, i, j); // 交换这两个元素
Swap(nums, i, j); // 交换这两个元素
}
swap(nums, i, left); // 将基准数交换至两子数组的分界线
Swap(nums, i, left); // 将基准数交换至两子数组的分界线
return i; // 返回基准数的索引
}
/* 快速排序(尾递归优化) */
public static void quickSort(int[] nums, int left, int right) {
public static void QuickSort(int[] nums, int left, int right) {
// 子数组长度为 1 时终止
while (left < right) {
// 哨兵划分操作
int pivot = partition(nums, left, right);
int pivot = Partition(nums, left, right);
// 对两个子数组中较短的那个执行快排
if (pivot - left < right - pivot) {
quickSort(nums, left, pivot - 1); // 递归排序左子数组
QuickSort(nums, left, pivot - 1); // 递归排序左子数组
left = pivot + 1; // 剩余未排序区间为 [pivot + 1, right]
} else {
quickSort(nums, pivot + 1, right); // 递归排序右子数组
QuickSort(nums, pivot + 1, right); // 递归排序右子数组
right = pivot - 1; // 剩余未排序区间为 [left, pivot - 1]
}
}
@@ -142,17 +136,17 @@ public class quick_sort {
public void Test() {
/* 快速排序 */
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));
/* 快速排序(中位基准数优化) */
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));
/* 快速排序(尾递归优化) */
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));
}
}

View File

@@ -8,19 +8,19 @@ namespace hello_algo.chapter_sorting;
public class radix_sort {
/* 获取元素 num 的第 k 位,其中 exp = 10^(k-1) */
static int digit(int num, int exp) {
static int Digit(int num, int exp) {
// 传入 exp 而非 k 可以避免在此重复执行昂贵的次方计算
return (num / exp) % 10;
}
/* 计数排序(根据 nums 第 k 位排序) */
static void countingSortDigit(int[] nums, int exp) {
static void CountingSortDigit(int[] nums, int exp) {
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
int[] counter = new int[10];
int n = nums.Length;
// 统计 0~9 各数字的出现次数
for (int i = 0; i < n; i++) {
int d = digit(nums[i], exp); // 获取 nums[i] 第 k 位,记为 d
int d = Digit(nums[i], exp); // 获取 nums[i] 第 k 位,记为 d
counter[d]++; // 统计数字 d 的出现次数
}
// 求前缀和,将“出现个数”转换为“数组索引”
@@ -30,7 +30,7 @@ public class radix_sort {
// 倒序遍历,根据桶内统计结果,将各元素填入 res
int[] res = new int[n];
for (int i = n - 1; i >= 0; i--) {
int d = digit(nums[i], exp);
int d = Digit(nums[i], exp);
int j = counter[d] - 1; // 获取 d 在数组中的索引 j
res[j] = nums[i]; // 将当前元素填入索引 j
counter[d]--; // 将 d 的数量减 1
@@ -42,7 +42,7 @@ public class radix_sort {
}
/* 基数排序 */
static void radixSort(int[] nums) {
static void RadixSort(int[] nums) {
// 获取数组的最大元素,用于判断最大位数
int m = int.MinValue;
foreach (int num in nums) {
@@ -54,7 +54,7 @@ public class radix_sort {
// k = 1 -> exp = 1
// k = 2 -> exp = 10
// 即 exp = 10^(k-1)
countingSortDigit(nums, exp);
CountingSortDigit(nums, exp);
}
}
@@ -63,7 +63,7 @@ public class radix_sort {
// 基数排序
int[] nums = { 10546151, 35663510, 42865989, 34862445, 81883077,
88906420, 72429244, 30524779, 82060337, 63832996 };
radixSort(nums);
RadixSort(nums);
Console.WriteLine("基数排序完成后 nums = " + string.Join(" ", nums));
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_sorting;
public class selection_sort {
/* 选择排序 */
public static void selectionSort(int[] nums) {
public static void SelectionSort(int[] nums) {
int n = nums.Length;
// 外循环:未排序区间为 [i, n-1]
for (int i = 0; i < n - 1; i++) {
@@ -26,7 +26,7 @@ public class selection_sort {
[Test]
public void Test() {
int[] nums = { 4, 1, 3, 1, 5, 2 };
selectionSort(nums);
SelectionSort(nums);
Console.WriteLine("选择排序完成后 nums = " + string.Join(" ", nums));
}
}

View File

@@ -19,95 +19,95 @@ public class ArrayDeque {
}
/* 获取双向队列的容量 */
public int capacity() {
public int Capacity() {
return nums.Length;
}
/* 获取双向队列的长度 */
public int size() {
public int Size() {
return queSize;
}
/* 判断双向队列是否为空 */
public bool isEmpty() {
public bool IsEmpty() {
return queSize == 0;
}
/* 计算环形数组索引 */
private int index(int i) {
private int Index(int i) {
// 通过取余操作实现数组首尾相连
// 当 i 越过数组尾部后,回到头部
// 当 i 越过数组头部后,回到尾部
return (i + capacity()) % capacity();
return (i + Capacity()) % Capacity();
}
/* 队首入队 */
public void pushFirst(int num) {
if (queSize == capacity()) {
public void PushFirst(int num) {
if (queSize == Capacity()) {
Console.WriteLine("双向队列已满");
return;
}
// 队首指针向左移动一位
// 通过取余操作,实现 front 越过数组头部后回到尾部
front = index(front - 1);
front = Index(front - 1);
// 将 num 添加至队首
nums[front] = num;
queSize++;
}
/* 队尾入队 */
public void pushLast(int num) {
if (queSize == capacity()) {
public void PushLast(int num) {
if (queSize == Capacity()) {
Console.WriteLine("双向队列已满");
return;
}
// 计算尾指针,指向队尾索引 + 1
int rear = index(front + queSize);
int rear = Index(front + queSize);
// 将 num 添加至队尾
nums[rear] = num;
queSize++;
}
/* 队首出队 */
public int popFirst() {
int num = peekFirst();
public int PopFirst() {
int num = PeekFirst();
// 队首指针向后移动一位
front = index(front + 1);
front = Index(front + 1);
queSize--;
return num;
}
/* 队尾出队 */
public int popLast() {
int num = peekLast();
public int PopLast() {
int num = PeekLast();
queSize--;
return num;
}
/* 访问队首元素 */
public int peekFirst() {
if (isEmpty()) {
public int PeekFirst() {
if (IsEmpty()) {
throw new InvalidOperationException();
}
return nums[front];
}
/* 访问队尾元素 */
public int peekLast() {
if (isEmpty()) {
public int PeekLast() {
if (IsEmpty()) {
throw new InvalidOperationException();
}
// 计算尾元素索引
int last = index(front + queSize - 1);
int last = Index(front + queSize - 1);
return nums[last];
}
/* 返回数组用于打印 */
public int[] toArray() {
public int[] ToArray() {
// 仅转换有效长度范围内的列表元素
int[] res = new int[queSize];
for (int i = 0, j = front; i < queSize; i++, j++) {
res[i] = nums[index(j)];
res[i] = nums[Index(j)];
}
return res;
}
@@ -117,36 +117,36 @@ public class array_deque {
[Test]
public void Test() {
/* 初始化双向队列 */
ArrayDeque deque = new ArrayDeque(10);
deque.pushLast(3);
deque.pushLast(2);
deque.pushLast(5);
Console.WriteLine("双向队列 deque = " + string.Join(" ", deque.toArray()));
ArrayDeque deque = new(10);
deque.PushLast(3);
deque.PushLast(2);
deque.PushLast(5);
Console.WriteLine("双向队列 deque = " + string.Join(" ", deque.ToArray()));
/* 访问元素 */
int peekFirst = deque.peekFirst();
int peekFirst = deque.PeekFirst();
Console.WriteLine("队首元素 peekFirst = " + peekFirst);
int peekLast = deque.peekLast();
int peekLast = deque.PeekLast();
Console.WriteLine("队尾元素 peekLast = " + peekLast);
/* 元素入队 */
deque.pushLast(4);
Console.WriteLine("元素 4 队尾入队后 deque = " + string.Join(" ", deque.toArray()));
deque.pushFirst(1);
Console.WriteLine("元素 1 队首入队后 deque = " + string.Join(" ", deque.toArray()));
deque.PushLast(4);
Console.WriteLine("元素 4 队尾入队后 deque = " + string.Join(" ", deque.ToArray()));
deque.PushFirst(1);
Console.WriteLine("元素 1 队首入队后 deque = " + string.Join(" ", deque.ToArray()));
/* 元素出队 */
int popLast = deque.popLast();
Console.WriteLine("队尾出队元素 = " + popLast + ",队尾出队后 deque = " + string.Join(" ", deque.toArray()));
int popFirst = deque.popFirst();
Console.WriteLine("队首出队元素 = " + popFirst + ",队首出队后 deque = " + string.Join(" ", deque.toArray()));
int popLast = deque.PopLast();
Console.WriteLine("队尾出队元素 = " + popLast + ",队尾出队后 deque = " + string.Join(" ", deque.ToArray()));
int popFirst = deque.PopFirst();
Console.WriteLine("队首出队元素 = " + popFirst + ",队首出队后 deque = " + string.Join(" ", deque.ToArray()));
/* 获取双向队列的长度 */
int size = deque.size();
int size = deque.Size();
Console.WriteLine("双向队列长度 size = " + size);
/* 判断双向队列是否为空 */
bool isEmpty = deque.isEmpty();
bool isEmpty = deque.IsEmpty();
Console.WriteLine("双向队列是否为空 = " + isEmpty);
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_stack_and_queue;
/* 基于环形数组实现的队列 */
class ArrayQueue {
private int[] nums; // 用于存储队列元素的数组
private readonly int[] nums; // 用于存储队列元素的数组
private int front; // 队首指针,指向队首元素
private int queSize; // 队列长度
@@ -18,56 +18,56 @@ class ArrayQueue {
}
/* 获取队列的容量 */
public int capacity() {
public int Capacity() {
return nums.Length;
}
/* 获取队列的长度 */
public int size() {
public int Size() {
return queSize;
}
/* 判断队列是否为空 */
public bool isEmpty() {
public bool IsEmpty() {
return queSize == 0;
}
/* 入队 */
public void push(int num) {
if (queSize == capacity()) {
public void Push(int num) {
if (queSize == Capacity()) {
Console.WriteLine("队列已满");
return;
}
// 计算尾指针,指向队尾索引 + 1
// 通过取余操作,实现 rear 越过数组尾部后回到头部
int rear = (front + queSize) % capacity();
int rear = (front + queSize) % Capacity();
// 将 num 添加至队尾
nums[rear] = num;
queSize++;
}
/* 出队 */
public int pop() {
int num = peek();
public int Pop() {
int num = Peek();
// 队首指针向后移动一位,若越过尾部则返回到数组头部
front = (front + 1) % capacity();
front = (front + 1) % Capacity();
queSize--;
return num;
}
/* 访问队首元素 */
public int peek() {
if (isEmpty())
public int Peek() {
if (IsEmpty())
throw new Exception();
return nums[front];
}
/* 返回数组 */
public int[] toArray() {
public int[] ToArray() {
// 仅转换有效长度范围内的列表元素
int[] res = new int[queSize];
for (int i = 0, j = front; i < queSize; i++, j++) {
res[i] = nums[j % this.capacity()];
res[i] = nums[j % this.Capacity()];
}
return res;
}
@@ -78,37 +78,37 @@ public class array_queue {
public void Test() {
/* 初始化队列 */
int capacity = 10;
ArrayQueue queue = new ArrayQueue(capacity);
ArrayQueue queue = new(capacity);
/* 元素入队 */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
Console.WriteLine("队列 queue = " + string.Join(",", queue.toArray()));
queue.Push(1);
queue.Push(3);
queue.Push(2);
queue.Push(5);
queue.Push(4);
Console.WriteLine("队列 queue = " + string.Join(",", queue.ToArray()));
/* 访问队首元素 */
int peek = queue.peek();
int peek = queue.Peek();
Console.WriteLine("队首元素 peek = " + peek);
/* 元素出队 */
int pop = queue.pop();
Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + string.Join(",", queue.toArray()));
int pop = queue.Pop();
Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + string.Join(",", queue.ToArray()));
/* 获取队列的长度 */
int size = queue.size();
int size = queue.Size();
Console.WriteLine("队列长度 size = " + size);
/* 判断队列是否为空 */
bool isEmpty = queue.isEmpty();
bool isEmpty = queue.IsEmpty();
Console.WriteLine("队列是否为空 = " + isEmpty);
/* 测试环形数组 */
for (int i = 0; i < 10; i++) {
queue.push(i);
queue.pop();
Console.WriteLine("第 " + i + " 轮入队 + 出队后 queue = " + string.Join(",", queue.toArray()));
queue.Push(i);
queue.Pop();
Console.WriteLine("第 " + i + " 轮入队 + 出队后 queue = " + string.Join(",", queue.ToArray()));
}
}
}

View File

@@ -8,45 +8,45 @@ namespace hello_algo.chapter_stack_and_queue;
/* 基于数组实现的栈 */
class ArrayStack {
private List<int> stack;
private readonly List<int> stack;
public ArrayStack() {
// 初始化列表(动态数组)
stack = new();
}
/* 获取栈的长度 */
public int size() {
return stack.Count();
public int Size() {
return stack.Count;
}
/* 判断栈是否为空 */
public bool isEmpty() {
return size() == 0;
public bool IsEmpty() {
return Size() == 0;
}
/* 入栈 */
public void push(int num) {
public void Push(int num) {
stack.Add(num);
}
/* 出栈 */
public int pop() {
if (isEmpty())
public int Pop() {
if (IsEmpty())
throw new Exception();
var val = peek();
stack.RemoveAt(size() - 1);
var val = Peek();
stack.RemoveAt(Size() - 1);
return val;
}
/* 访问栈顶元素 */
public int peek() {
if (isEmpty())
public int Peek() {
if (IsEmpty())
throw new Exception();
return stack[size() - 1];
return stack[Size() - 1];
}
/* 将 List 转化为 Array 并返回 */
public int[] toArray() {
public int[] ToArray() {
return stack.ToArray();
}
}
@@ -55,30 +55,30 @@ public class array_stack {
[Test]
public void Test() {
/* 初始化栈 */
ArrayStack stack = new ArrayStack();
ArrayStack stack = new();
/* 元素入栈 */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
Console.WriteLine("栈 stack = " + string.Join(",", stack.toArray()));
stack.Push(1);
stack.Push(3);
stack.Push(2);
stack.Push(5);
stack.Push(4);
Console.WriteLine("栈 stack = " + string.Join(",", stack.ToArray()));
/* 访问栈顶元素 */
int peek = stack.peek();
int peek = stack.Peek();
Console.WriteLine("栈顶元素 peek = " + peek);
/* 元素出栈 */
int pop = stack.pop();
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack.toArray()));
int pop = stack.Pop();
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack.ToArray()));
/* 获取栈的长度 */
int size = stack.size();
int size = stack.Size();
Console.WriteLine("栈的长度 size = " + size);
/* 判断是否为空 */
bool isEmpty = stack.isEmpty();
bool isEmpty = stack.IsEmpty();
Console.WriteLine("栈是否为空 = " + isEmpty);
}
}

View File

@@ -11,7 +11,7 @@ public class deque {
public void Test() {
/* 初始化双向队列 */
// 在 C# 中,将链表 LinkedList 看作双向队列来使用
LinkedList<int> deque = new LinkedList<int>();
LinkedList<int> deque = new();
/* 元素入队 */
deque.AddLast(2); // 添加至队尾
@@ -22,9 +22,9 @@ public class deque {
Console.WriteLine("双向队列 deque = " + string.Join(",", deque));
/* 访问元素 */
int peekFirst = deque.First.Value; // 队首元素
int? peekFirst = deque.First?.Value; // 队首元素
Console.WriteLine("队首元素 peekFirst = " + peekFirst);
int peekLast = deque.Last.Value; // 队尾元素
int? peekLast = deque.Last?.Value; // 队尾元素
Console.WriteLine("队尾元素 peekLast = " + peekLast);
/* 元素出队 */

View File

@@ -30,20 +30,20 @@ public class LinkedListDeque {
}
/* 获取双向队列的长度 */
public int size() {
public int Size() {
return queSize;
}
/* 判断双向队列是否为空 */
public bool isEmpty() {
return size() == 0;
public bool IsEmpty() {
return Size() == 0;
}
/* 入队操作 */
private void push(int num, bool isFront) {
ListNode node = new ListNode(num);
private void Push(int num, bool isFront) {
ListNode node = new(num);
// 若链表为空,则令 front, rear 都指向 node
if (isEmpty()) {
if (IsEmpty()) {
front = node;
rear = node;
}
@@ -66,25 +66,25 @@ public class LinkedListDeque {
}
/* 队首入队 */
public void pushFirst(int num) {
push(num, true);
public void PushFirst(int num) {
Push(num, true);
}
/* 队尾入队 */
public void pushLast(int num) {
push(num, false);
public void PushLast(int num) {
Push(num, false);
}
/* 出队操作 */
private int? pop(bool isFront) {
if (isEmpty())
private int? Pop(bool isFront) {
if (IsEmpty())
throw new Exception();
int val;
int? val;
// 队首出队操作
if (isFront) {
val = front.val; // 暂存头节点值
val = front?.val; // 暂存头节点值
// 删除头节点
ListNode fNext = front.next;
ListNode? fNext = front?.next;
if (fNext != null) {
fNext.prev = null;
front.next = null;
@@ -93,9 +93,9 @@ public class LinkedListDeque {
}
// 队尾出队操作
else {
val = rear.val; // 暂存尾节点值
val = rear?.val; // 暂存尾节点值
// 删除尾节点
ListNode rPrev = rear.prev;
ListNode? rPrev = rear?.prev;
if (rPrev != null) {
rPrev.next = null;
rear.prev = null;
@@ -108,36 +108,36 @@ public class LinkedListDeque {
}
/* 队首出队 */
public int? popFirst() {
return pop(true);
public int? PopFirst() {
return Pop(true);
}
/* 队尾出队 */
public int? popLast() {
return pop(false);
public int? PopLast() {
return Pop(false);
}
/* 访问队首元素 */
public int? peekFirst() {
if (isEmpty())
public int? PeekFirst() {
if (IsEmpty())
throw new Exception();
return front.val;
return front?.val;
}
/* 访问队尾元素 */
public int? peekLast() {
if (isEmpty())
public int? PeekLast() {
if (IsEmpty())
throw new Exception();
return rear.val;
return rear?.val;
}
/* 返回数组用于打印 */
public int[] toArray() {
ListNode node = front;
int[] res = new int[size()];
public int?[] ToArray() {
ListNode? node = front;
int?[] res = new int?[Size()];
for (int i = 0; i < res.Length; i++) {
res[i] = node.val;
node = node.next;
res[i] = node?.val;
node = node?.next;
}
return res;
@@ -148,36 +148,36 @@ public class linkedlist_deque {
[Test]
public void Test() {
/* 初始化双向队列 */
LinkedListDeque deque = new LinkedListDeque();
deque.pushLast(3);
deque.pushLast(2);
deque.pushLast(5);
Console.WriteLine("双向队列 deque = " + string.Join(" ", deque.toArray()));
LinkedListDeque deque = new();
deque.PushLast(3);
deque.PushLast(2);
deque.PushLast(5);
Console.WriteLine("双向队列 deque = " + string.Join(" ", deque.ToArray()));
/* 访问元素 */
int? peekFirst = deque.peekFirst();
int? peekFirst = deque.PeekFirst();
Console.WriteLine("队首元素 peekFirst = " + peekFirst);
int? peekLast = deque.peekLast();
int? peekLast = deque.PeekLast();
Console.WriteLine("队尾元素 peekLast = " + peekLast);
/* 元素入队 */
deque.pushLast(4);
Console.WriteLine("元素 4 队尾入队后 deque = " + string.Join(" ", deque.toArray()));
deque.pushFirst(1);
Console.WriteLine("元素 1 队首入队后 deque = " + string.Join(" ", deque.toArray()));
deque.PushLast(4);
Console.WriteLine("元素 4 队尾入队后 deque = " + string.Join(" ", deque.ToArray()));
deque.PushFirst(1);
Console.WriteLine("元素 1 队首入队后 deque = " + string.Join(" ", deque.ToArray()));
/* 元素出队 */
int? popLast = deque.popLast();
Console.WriteLine("队尾出队元素 = " + popLast + ",队尾出队后 deque = " + string.Join(" ", deque.toArray()));
int? popFirst = deque.popFirst();
Console.WriteLine("队首出队元素 = " + popFirst + ",队首出队后 deque = " + string.Join(" ", deque.toArray()));
int? popLast = deque.PopLast();
Console.WriteLine("队尾出队元素 = " + popLast + ",队尾出队后 deque = " + string.Join(" ", deque.ToArray()));
int? popFirst = deque.PopFirst();
Console.WriteLine("队首出队元素 = " + popFirst + ",队首出队后 deque = " + string.Join(" ", deque.ToArray()));
/* 获取双向队列的长度 */
int size = deque.size();
int size = deque.Size();
Console.WriteLine("双向队列长度 size = " + size);
/* 判断双向队列是否为空 */
bool isEmpty = deque.isEmpty();
bool isEmpty = deque.IsEmpty();
Console.WriteLine("双向队列是否为空 = " + isEmpty);
}
}

View File

@@ -17,19 +17,19 @@ class LinkedListQueue {
}
/* 获取队列的长度 */
public int size() {
public int Size() {
return queSize;
}
/* 判断队列是否为空 */
public bool isEmpty() {
return size() == 0;
public bool IsEmpty() {
return Size() == 0;
}
/* 入队 */
public void push(int num) {
public void Push(int num) {
// 尾节点后添加 num
ListNode node = new ListNode(num);
ListNode node = new(num);
// 如果队列为空,则令头、尾节点都指向该节点
if (front == null) {
front = node;
@@ -43,8 +43,8 @@ class LinkedListQueue {
}
/* 出队 */
public int pop() {
int num = peek();
public int Pop() {
int num = Peek();
// 删除头节点
front = front?.next;
queSize--;
@@ -52,19 +52,19 @@ class LinkedListQueue {
}
/* 访问队首元素 */
public int peek() {
if (isEmpty())
public int Peek() {
if (IsEmpty())
throw new Exception();
return front.val;
}
/* 将链表转化为 Array 并返回 */
public int[] toArray() {
public int[] ToArray() {
if (front == null)
return Array.Empty<int>();
ListNode node = front;
int[] res = new int[size()];
int[] res = new int[Size()];
for (int i = 0; i < res.Length; i++) {
res[i] = node.val;
node = node.next;
@@ -77,30 +77,30 @@ public class linkedlist_queue {
[Test]
public void Test() {
/* 初始化队列 */
LinkedListQueue queue = new LinkedListQueue();
LinkedListQueue queue = new();
/* 元素入队 */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
Console.WriteLine("队列 queue = " + string.Join(",", queue.toArray()));
queue.Push(1);
queue.Push(3);
queue.Push(2);
queue.Push(5);
queue.Push(4);
Console.WriteLine("队列 queue = " + string.Join(",", queue.ToArray()));
/* 访问队首元素 */
int peek = queue.peek();
int peek = queue.Peek();
Console.WriteLine("队首元素 peek = " + peek);
/* 元素出队 */
int pop = queue.pop();
Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + string.Join(",", queue.toArray()));
int pop = queue.Pop();
Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + string.Join(",", queue.ToArray()));
/* 获取队列的长度 */
int size = queue.size();
int size = queue.Size();
Console.WriteLine("队列长度 size = " + size);
/* 判断队列是否为空 */
bool isEmpty = queue.isEmpty();
bool isEmpty = queue.IsEmpty();
Console.WriteLine("队列是否为空 = " + isEmpty);
}
}

View File

@@ -16,45 +16,46 @@ class LinkedListStack {
}
/* 获取栈的长度 */
public int size() {
public int Size() {
return stkSize;
}
/* 判断栈是否为空 */
public bool isEmpty() {
return size() == 0;
public bool IsEmpty() {
return Size() == 0;
}
/* 入栈 */
public void push(int num) {
ListNode node = new ListNode(num);
node.next = stackPeek;
public void Push(int num) {
ListNode node = new(num) {
next = stackPeek
};
stackPeek = node;
stkSize++;
}
/* 出栈 */
public int pop() {
int num = peek();
public int Pop() {
int num = Peek();
stackPeek = stackPeek.next;
stkSize--;
return num;
}
/* 访问栈顶元素 */
public int peek() {
if (isEmpty())
public int Peek() {
if (IsEmpty())
throw new Exception();
return stackPeek.val;
}
/* 将 List 转化为 Array 并返回 */
public int[] toArray() {
public int[] ToArray() {
if (stackPeek == null)
return Array.Empty<int>();
ListNode node = stackPeek;
int[] res = new int[size()];
int[] res = new int[Size()];
for (int i = res.Length - 1; i >= 0; i--) {
res[i] = node.val;
node = node.next;
@@ -67,30 +68,30 @@ public class linkedlist_stack {
[Test]
public void Test() {
/* 初始化栈 */
LinkedListStack stack = new LinkedListStack();
LinkedListStack stack = new();
/* 元素入栈 */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
Console.WriteLine("栈 stack = " + string.Join(",", stack.toArray()));
stack.Push(1);
stack.Push(3);
stack.Push(2);
stack.Push(5);
stack.Push(4);
Console.WriteLine("栈 stack = " + string.Join(",", stack.ToArray()));
/* 访问栈顶元素 */
int peek = stack.peek();
int peek = stack.Peek();
Console.WriteLine("栈顶元素 peek = " + peek);
/* 元素出栈 */
int pop = stack.pop();
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack.toArray()));
int pop = stack.Pop();
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack.ToArray()));
/* 获取栈的长度 */
int size = stack.size();
int size = stack.Size();
Console.WriteLine("栈的长度 size = " + size);
/* 判断是否为空 */
bool isEmpty = stack.isEmpty();
bool isEmpty = stack.IsEmpty();
Console.WriteLine("栈是否为空 = " + isEmpty);
}
}

View File

@@ -8,7 +8,7 @@ namespace hello_algo.chapter_tree;
/* 数组表示下的二叉树类 */
public class ArrayBinaryTree {
private List<int?> tree;
private readonly List<int?> tree;
/* 构造方法 */
public ArrayBinaryTree(List<int?> arr) {
@@ -16,80 +16,80 @@ public class ArrayBinaryTree {
}
/* 节点数量 */
public int size() {
public int Size() {
return tree.Count;
}
/* 获取索引为 i 节点的值 */
public int? val(int i) {
public int? Val(int i) {
// 若索引越界,则返回 null ,代表空位
if (i < 0 || i >= size())
if (i < 0 || i >= Size())
return null;
return tree[i];
}
/* 获取索引为 i 节点的左子节点的索引 */
public int left(int i) {
public int Left(int i) {
return 2 * i + 1;
}
/* 获取索引为 i 节点的右子节点的索引 */
public int right(int i) {
public int Right(int i) {
return 2 * i + 2;
}
/* 获取索引为 i 节点的父节点的索引 */
public int parent(int i) {
public int Parent(int i) {
return (i - 1) / 2;
}
/* 层序遍历 */
public List<int> levelOrder() {
List<int> res = new List<int>();
public List<int> LevelOrder() {
List<int> res = new();
// 直接遍历数组
for (int i = 0; i < size(); i++) {
if (val(i).HasValue)
res.Add(val(i).Value);
for (int i = 0; i < Size(); i++) {
if (Val(i).HasValue)
res.Add(Val(i).Value);
}
return res;
}
/* 深度优先遍历 */
private void dfs(int i, string order, List<int> res) {
private void Dfs(int i, string order, List<int> res) {
// 若为空位,则返回
if (!val(i).HasValue)
if (!Val(i).HasValue)
return;
// 前序遍历
if (order == "pre")
res.Add(val(i).Value);
dfs(left(i), order, res);
res.Add(Val(i).Value);
Dfs(Left(i), order, res);
// 中序遍历
if (order == "in")
res.Add(val(i).Value);
dfs(right(i), order, res);
res.Add(Val(i).Value);
Dfs(Right(i), order, res);
// 后序遍历
if (order == "post")
res.Add(val(i).Value);
res.Add(Val(i).Value);
}
/* 前序遍历 */
public List<int> preOrder() {
List<int> res = new List<int>();
dfs(0, "pre", res);
public List<int> PreOrder() {
List<int> res = new();
Dfs(0, "pre", res);
return res;
}
/* 中序遍历 */
public List<int> inOrder() {
List<int> res = new List<int>();
dfs(0, "in", res);
public List<int> InOrder() {
List<int> res = new();
Dfs(0, "in", res);
return res;
}
/* 后序遍历 */
public List<int> postOrder() {
List<int> res = new List<int>();
dfs(0, "post", res);
public List<int> PostOrder() {
List<int> res = new();
Dfs(0, "post", res);
return res;
}
}
@@ -99,7 +99,7 @@ public class array_binary_tree {
public void Test() {
// 初始化二叉树
// 这里借助了一个从数组直接生成二叉树的函数
List<int?> arr = new List<int?> { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };
List<int?> arr = new() { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };
TreeNode root = TreeNode.ListToTree(arr);
Console.WriteLine("\n初始化二叉树\n");
@@ -109,26 +109,26 @@ public class array_binary_tree {
PrintUtil.PrintTree(root);
// 数组表示下的二叉树类
ArrayBinaryTree abt = new ArrayBinaryTree(arr);
ArrayBinaryTree abt = new(arr);
// 访问节点
int i = 1;
int l = abt.left(i);
int r = abt.right(i);
int p = abt.parent(i);
Console.WriteLine("\n当前节点的索引为 " + i + " ,值为 " + abt.val(i));
Console.WriteLine("其左子节点的索引为 " + l + " ,值为 " + (abt.val(l).HasValue ? abt.val(l) : "null"));
Console.WriteLine("其右子节点的索引为 " + r + " ,值为 " + (abt.val(r).HasValue ? abt.val(r) : "null"));
Console.WriteLine("其父节点的索引为 " + p + " ,值为 " + (abt.val(p).HasValue ? abt.val(p) : "null"));
int l = abt.Left(i);
int r = abt.Right(i);
int p = abt.Parent(i);
Console.WriteLine("\n当前节点的索引为 " + i + " ,值为 " + abt.Val(i));
Console.WriteLine("其左子节点的索引为 " + l + " ,值为 " + (abt.Val(l).HasValue ? abt.Val(l) : "null"));
Console.WriteLine("其右子节点的索引为 " + r + " ,值为 " + (abt.Val(r).HasValue ? abt.Val(r) : "null"));
Console.WriteLine("其父节点的索引为 " + p + " ,值为 " + (abt.Val(p).HasValue ? abt.Val(p) : "null"));
// 遍历树
List<int> res = abt.levelOrder();
List<int> res = abt.LevelOrder();
Console.WriteLine("\n层序遍历为" + res.PrintList());
res = abt.preOrder();
res = abt.PreOrder();
Console.WriteLine("前序遍历为:" + res.PrintList());
res = abt.inOrder();
res = abt.InOrder();
Console.WriteLine("中序遍历为:" + res.PrintList());
res = abt.postOrder();
res = abt.PostOrder();
Console.WriteLine("后序遍历为:" + res.PrintList());
}
}

View File

@@ -11,77 +11,77 @@ class AVLTree {
public TreeNode? root; // 根节点
/* 获取节点高度 */
public int height(TreeNode? node) {
public int Height(TreeNode? node) {
// 空节点高度为 -1 ,叶节点高度为 0
return node == null ? -1 : node.height;
}
/* 更新节点高度 */
private void updateHeight(TreeNode node) {
private void UpdateHeight(TreeNode node) {
// 节点高度等于最高子树高度 + 1
node.height = Math.Max(height(node.left), height(node.right)) + 1;
node.height = Math.Max(Height(node.left), Height(node.right)) + 1;
}
/* 获取平衡因子 */
public int balanceFactor(TreeNode? node) {
public int BalanceFactor(TreeNode? node) {
// 空节点平衡因子为 0
if (node == null) return 0;
// 节点平衡因子 = 左子树高度 - 右子树高度
return height(node.left) - height(node.right);
return Height(node.left) - Height(node.right);
}
/* 右旋操作 */
TreeNode? rightRotate(TreeNode? node) {
TreeNode? RightRotate(TreeNode? node) {
TreeNode? child = node.left;
TreeNode? grandChild = child?.right;
// 以 child 为原点,将 node 向右旋转
child.right = node;
node.left = grandChild;
// 更新节点高度
updateHeight(node);
updateHeight(child);
UpdateHeight(node);
UpdateHeight(child);
// 返回旋转后子树的根节点
return child;
}
/* 左旋操作 */
TreeNode? leftRotate(TreeNode? node) {
TreeNode? LeftRotate(TreeNode? node) {
TreeNode? child = node.right;
TreeNode? grandChild = child?.left;
// 以 child 为原点,将 node 向左旋转
child.left = node;
node.right = grandChild;
// 更新节点高度
updateHeight(node);
updateHeight(child);
UpdateHeight(node);
UpdateHeight(child);
// 返回旋转后子树的根节点
return child;
}
/* 执行旋转操作,使该子树重新恢复平衡 */
TreeNode? rotate(TreeNode? node) {
TreeNode? Rotate(TreeNode? node) {
// 获取节点 node 的平衡因子
int balanceFactorInt = balanceFactor(node);
int balanceFactorInt = BalanceFactor(node);
// 左偏树
if (balanceFactorInt > 1) {
if (balanceFactor(node.left) >= 0) {
if (BalanceFactor(node.left) >= 0) {
// 右旋
return rightRotate(node);
return RightRotate(node);
} else {
// 先左旋后右旋
node.left = leftRotate(node?.left);
return rightRotate(node);
node.left = LeftRotate(node?.left);
return RightRotate(node);
}
}
// 右偏树
if (balanceFactorInt < -1) {
if (balanceFactor(node.right) <= 0) {
if (BalanceFactor(node.right) <= 0) {
// 左旋
return leftRotate(node);
return LeftRotate(node);
} else {
// 先右旋后左旋
node.right = rightRotate(node?.right);
return leftRotate(node);
node.right = RightRotate(node?.right);
return LeftRotate(node);
}
}
// 平衡树,无须旋转,直接返回
@@ -89,43 +89,43 @@ class AVLTree {
}
/* 插入节点 */
public void insert(int val) {
root = insertHelper(root, val);
public void Insert(int val) {
root = InsertHelper(root, val);
}
/* 递归插入节点(辅助方法) */
private TreeNode? insertHelper(TreeNode? node, int val) {
private TreeNode? InsertHelper(TreeNode? node, int val) {
if (node == null) return new TreeNode(val);
/* 1. 查找插入位置,并插入节点 */
if (val < node.val)
node.left = insertHelper(node.left, val);
node.left = InsertHelper(node.left, val);
else if (val > node.val)
node.right = insertHelper(node.right, val);
node.right = InsertHelper(node.right, val);
else
return node; // 重复节点不插入,直接返回
updateHeight(node); // 更新节点高度
UpdateHeight(node); // 更新节点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
node = Rotate(node);
// 返回子树的根节点
return node;
}
/* 删除节点 */
public void remove(int val) {
root = removeHelper(root, val);
public void Remove(int val) {
root = RemoveHelper(root, val);
}
/* 递归删除节点(辅助方法) */
private TreeNode? removeHelper(TreeNode? node, int val) {
private TreeNode? RemoveHelper(TreeNode? node, int val) {
if (node == null) return null;
/* 1. 查找节点,并删除之 */
if (val < node.val)
node.left = removeHelper(node.left, val);
node.left = RemoveHelper(node.left, val);
else if (val > node.val)
node.right = removeHelper(node.right, val);
node.right = RemoveHelper(node.right, val);
else {
if (node.left == null || node.right == null) {
TreeNode? child = node.left != null ? node.left : node.right;
TreeNode? child = node.left ?? node.right;
// 子节点数量 = 0 ,直接删除 node 并返回
if (child == null)
return null;
@@ -138,19 +138,19 @@ class AVLTree {
while (temp.left != null) {
temp = temp.left;
}
node.right = removeHelper(node.right, temp.val);
node.right = RemoveHelper(node.right, temp.val);
node.val = temp.val;
}
}
updateHeight(node); // 更新节点高度
UpdateHeight(node); // 更新节点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
node = Rotate(node);
// 返回子树的根节点
return node;
}
/* 查找节点 */
public TreeNode? search(int val) {
public TreeNode? Search(int val) {
TreeNode? cur = root;
// 循环查找,越过叶节点后跳出
while (cur != null) {
@@ -170,14 +170,14 @@ class AVLTree {
}
public class avl_tree {
static void testInsert(AVLTree tree, int val) {
tree.insert(val);
static void TestInsert(AVLTree tree, int val) {
tree.Insert(val);
Console.WriteLine("\n插入节点 " + val + " 后AVL 树为");
PrintUtil.PrintTree(tree.root);
}
static void testRemove(AVLTree tree, int val) {
tree.remove(val);
static void TestRemove(AVLTree tree, int val) {
tree.Remove(val);
Console.WriteLine("\n删除节点 " + val + " 后AVL 树为");
PrintUtil.PrintTree(tree.root);
}
@@ -185,32 +185,32 @@ public class avl_tree {
[Test]
public void Test() {
/* 初始化空 AVL 树 */
AVLTree avlTree = new AVLTree();
AVLTree avlTree = new();
/* 插入节点 */
// 请关注插入节点后AVL 树是如何保持平衡的
testInsert(avlTree, 1);
testInsert(avlTree, 2);
testInsert(avlTree, 3);
testInsert(avlTree, 4);
testInsert(avlTree, 5);
testInsert(avlTree, 8);
testInsert(avlTree, 7);
testInsert(avlTree, 9);
testInsert(avlTree, 10);
testInsert(avlTree, 6);
TestInsert(avlTree, 1);
TestInsert(avlTree, 2);
TestInsert(avlTree, 3);
TestInsert(avlTree, 4);
TestInsert(avlTree, 5);
TestInsert(avlTree, 8);
TestInsert(avlTree, 7);
TestInsert(avlTree, 9);
TestInsert(avlTree, 10);
TestInsert(avlTree, 6);
/* 插入重复节点 */
testInsert(avlTree, 7);
TestInsert(avlTree, 7);
/* 删除节点 */
// 请关注删除节点后AVL 树是如何保持平衡的
testRemove(avlTree, 8); // 删除度为 0 的节点
testRemove(avlTree, 5); // 删除度为 1 的节点
testRemove(avlTree, 4); // 删除度为 2 的节点
TestRemove(avlTree, 8); // 删除度为 0 的节点
TestRemove(avlTree, 5); // 删除度为 1 的节点
TestRemove(avlTree, 4); // 删除度为 2 的节点
/* 查询节点 */
TreeNode? node = avlTree.search(7);
TreeNode? node = avlTree.Search(7);
Console.WriteLine("\n查找到的节点对象为 " + node + ",节点值 = " + node?.val);
}
}

View File

@@ -15,12 +15,12 @@ class BinarySearchTree {
}
/* 获取二叉树根节点 */
public TreeNode? getRoot() {
public TreeNode? GetRoot() {
return root;
}
/* 查找节点 */
public TreeNode? search(int num) {
public TreeNode? Search(int num) {
TreeNode? cur = root;
// 循环查找,越过叶节点后跳出
while (cur != null) {
@@ -39,7 +39,7 @@ class BinarySearchTree {
}
/* 插入节点 */
public void insert(int num) {
public void Insert(int num) {
// 若树为空,则初始化根节点
if (root == null) {
root = new TreeNode(num);
@@ -61,7 +61,7 @@ class BinarySearchTree {
}
// 插入节点
TreeNode node = new TreeNode(num);
TreeNode node = new(num);
if (pre != null) {
if (pre.val < num)
pre.right = node;
@@ -72,7 +72,7 @@ class BinarySearchTree {
/* 删除节点 */
public void remove(int num) {
public void Remove(int num) {
// 若树为空,直接提前返回
if (root == null)
return;
@@ -96,7 +96,7 @@ class BinarySearchTree {
// 子节点数量 = 0 or 1
if (cur.left == null || cur.right == null) {
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
TreeNode? child = cur.left != null ? cur.left : cur.right;
TreeNode? child = cur.left ?? cur.right;
// 删除节点 cur
if (cur != root) {
if (pre.left == cur)
@@ -116,7 +116,7 @@ class BinarySearchTree {
tmp = tmp.left;
}
// 递归删除节点 tmp
remove(tmp.val);
Remove(tmp.val);
// 用 tmp 覆盖 cur
cur.val = tmp.val;
}
@@ -127,34 +127,34 @@ public class binary_search_tree {
[Test]
public void Test() {
/* 初始化二叉搜索树 */
BinarySearchTree bst = new BinarySearchTree();
BinarySearchTree bst = new();
// 请注意,不同的插入顺序会生成不同的二叉树,该序列可以生成一个完美二叉树
int[] nums = { 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 };
foreach (int num in nums) {
bst.insert(num);
bst.Insert(num);
}
Console.WriteLine("\n初始化的二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
PrintUtil.PrintTree(bst.GetRoot());
/* 查找节点 */
TreeNode? node = bst.search(7);
TreeNode? node = bst.Search(7);
Console.WriteLine("\n查找到的节点对象为 " + node + ",节点值 = " + node.val);
/* 插入节点 */
bst.insert(16);
bst.Insert(16);
Console.WriteLine("\n插入节点 16 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
PrintUtil.PrintTree(bst.GetRoot());
/* 删除节点 */
bst.remove(1);
bst.Remove(1);
Console.WriteLine("\n删除节点 1 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
bst.remove(2);
PrintUtil.PrintTree(bst.GetRoot());
bst.Remove(2);
Console.WriteLine("\n删除节点 2 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
bst.remove(4);
PrintUtil.PrintTree(bst.GetRoot());
bst.Remove(4);
Console.WriteLine("\n删除节点 4 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
PrintUtil.PrintTree(bst.GetRoot());
}
}

View File

@@ -11,11 +11,11 @@ public class binary_tree {
public void Test() {
/* 初始化二叉树 */
// 初始化节点
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
TreeNode n4 = new TreeNode(4);
TreeNode n5 = new TreeNode(5);
TreeNode n1 = new(1);
TreeNode n2 = new(2);
TreeNode n3 = new(3);
TreeNode n4 = new(4);
TreeNode n5 = new(5);
// 构建引用指向(即指针)
n1.left = n2;
n1.right = n3;
@@ -25,7 +25,7 @@ public class binary_tree {
PrintUtil.PrintTree(n1);
/* 插入与删除节点 */
TreeNode P = new TreeNode(0);
TreeNode P = new(0);
// 在 n1 -> n2 中间插入节点 P
n1.left = P;
P.left = n2;

View File

@@ -9,7 +9,7 @@ namespace hello_algo.chapter_tree;
public class binary_tree_bfs {
/* 层序遍历 */
public List<int> levelOrder(TreeNode root) {
public List<int> LevelOrder(TreeNode root) {
// 初始化队列,加入根节点
Queue<TreeNode> queue = new();
queue.Enqueue(root);
@@ -34,7 +34,7 @@ public class binary_tree_bfs {
Console.WriteLine("\n初始化二叉树\n");
PrintUtil.PrintTree(root);
List<int> list = levelOrder(root);
List<int> list = LevelOrder(root);
Console.WriteLine("\n层序遍历的节点打印序列 = " + string.Join(",", list));
}
}

View File

@@ -7,32 +7,32 @@
namespace hello_algo.chapter_tree;
public class binary_tree_dfs {
List<int> list = new();
readonly List<int> list = new();
/* 前序遍历 */
void preOrder(TreeNode? root) {
void PreOrder(TreeNode? root) {
if (root == null) return;
// 访问优先级:根节点 -> 左子树 -> 右子树
list.Add(root.val);
preOrder(root.left);
preOrder(root.right);
PreOrder(root.left);
PreOrder(root.right);
}
/* 中序遍历 */
void inOrder(TreeNode? root) {
void InOrder(TreeNode? root) {
if (root == null) return;
// 访问优先级:左子树 -> 根节点 -> 右子树
inOrder(root.left);
InOrder(root.left);
list.Add(root.val);
inOrder(root.right);
InOrder(root.right);
}
/* 后序遍历 */
void postOrder(TreeNode? root) {
void PostOrder(TreeNode? root) {
if (root == null) return;
// 访问优先级:左子树 -> 右子树 -> 根节点
postOrder(root.left);
postOrder(root.right);
PostOrder(root.left);
PostOrder(root.right);
list.Add(root.val);
}
@@ -45,15 +45,15 @@ public class binary_tree_dfs {
PrintUtil.PrintTree(root);
list.Clear();
preOrder(root);
PreOrder(root);
Console.WriteLine("\n前序遍历的节点打印序列 = " + string.Join(",", list));
list.Clear();
inOrder(root);
InOrder(root);
Console.WriteLine("\n中序遍历的节点打印序列 = " + string.Join(",", list));
list.Clear();
postOrder(root);
PostOrder(root);
Console.WriteLine("\n后序遍历的节点打印序列 = " + string.Join(",", list));
}
}

View File

@@ -15,7 +15,7 @@ public class ListNode {
/* Generate a linked list with an array */
public static ListNode? ArrToLinkedList(int[] arr) {
ListNode dum = new ListNode(0);
ListNode dum = new(0);
ListNode head = dum;
foreach (int val in arr) {
head.next = new ListNode(val);

View File

@@ -70,7 +70,7 @@ public static class PrintUtil {
}
string prev_str = " ";
Trunk trunk = new Trunk(prev, prev_str);
Trunk trunk = new(prev, prev_str);
PrintTree(root.right, trunk, true);
@@ -126,8 +126,8 @@ public static class PrintUtil {
public static void PrintHeap(PriorityQueue<int, int> queue) {
var newQueue = new PriorityQueue<int, int>(queue.UnorderedItems, queue.Comparer);
Console.Write("堆的数组表示:");
List<int> list = new List<int>();
while (newQueue.TryDequeue(out int element, out int priority)) {
List<int> list = new();
while (newQueue.TryDequeue(out int element, out _)) {
list.Add(element);
}
Console.WriteLine("堆的树状表示:");

View File

@@ -39,9 +39,10 @@ public class TreeNode {
if (i < 0 || i >= arr.Count || !arr[i].HasValue) {
return null;
}
TreeNode root = new TreeNode(arr[i].Value);
root.left = ListToTreeDFS(arr, 2 * i + 1);
root.right = ListToTreeDFS(arr, 2 * i + 2);
TreeNode root = new(arr[i].Value) {
left = ListToTreeDFS(arr, 2 * i + 1),
right = ListToTreeDFS(arr, 2 * i + 2)
};
return root;
}
@@ -63,8 +64,8 @@ public class TreeNode {
}
/* 将二叉树序列化为列表 */
public static List<int?> treeToList(TreeNode root) {
List<int?> res = new List<int?>();
public static List<int?> TreeToList(TreeNode root) {
List<int?> res = new();
TreeToListDFS(root, 0, res);
return res;
}

View File

@@ -24,7 +24,7 @@ public class Vertex {
/* 输入顶点列表 vets ,返回值列表 vals */
public static List<int> VetsToVals(List<Vertex> vets) {
List<int> vals = new List<int>();
List<int> vals = new();
foreach (Vertex vet in vets) {
vals.Add(vet.val);
}

View File

@@ -144,7 +144,7 @@
=== "C#"
```csharp title="array.cs"
[class]{array}-[func]{randomAccess}
[class]{array}-[func]{RandomAccess}
```
=== "Go"
@@ -224,7 +224,7 @@
=== "C#"
```csharp title="array.cs"
[class]{array}-[func]{insert}
[class]{array}-[func]{Insert}
```
=== "Go"
@@ -304,7 +304,7 @@
=== "C#"
```csharp title="array.cs"
[class]{array}-[func]{remove}
[class]{array}-[func]{Remove}
```
=== "Go"
@@ -386,7 +386,7 @@
=== "C#"
```csharp title="array.cs"
[class]{array}-[func]{traverse}
[class]{array}-[func]{Traverse}
```
=== "Go"
@@ -464,7 +464,7 @@
=== "C#"
```csharp title="array.cs"
[class]{array}-[func]{find}
[class]{array}-[func]{Find}
```
=== "Go"
@@ -542,7 +542,7 @@
=== "C#"
```csharp title="array.cs"
[class]{array}-[func]{extend}
[class]{array}-[func]{Extend}
```
=== "Go"

View File

@@ -246,11 +246,11 @@
```csharp title="linked_list.cs"
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
// 初始化各个节点
ListNode n0 = new ListNode(1);
ListNode n1 = new ListNode(3);
ListNode n2 = new ListNode(2);
ListNode n3 = new ListNode(5);
ListNode n4 = new ListNode(4);
ListNode n0 = new(1);
ListNode n1 = new(3);
ListNode n2 = new(2);
ListNode n3 = new(5);
ListNode n4 = new(4);
// 构建引用指向
n0.next = n1;
n1.next = n2;
@@ -426,7 +426,7 @@
=== "C#"
```csharp title="linked_list.cs"
[class]{linked_list}-[func]{insert}
[class]{linked_list}-[func]{Insert}
```
=== "Go"
@@ -506,7 +506,7 @@
=== "C#"
```csharp title="linked_list.cs"
[class]{linked_list}-[func]{remove}
[class]{linked_list}-[func]{Remove}
```
=== "Go"
@@ -582,7 +582,7 @@
=== "C#"
```csharp title="linked_list.cs"
[class]{linked_list}-[func]{access}
[class]{linked_list}-[func]{Access}
```
=== "Go"
@@ -658,7 +658,7 @@
=== "C#"
```csharp title="linked_list.cs"
[class]{linked_list}-[func]{find}
[class]{linked_list}-[func]{Find}
```
=== "Go"

View File

@@ -47,7 +47,7 @@
```csharp title="list.cs"
/* 初始化列表 */
// 无初始值
List<int> list1 = new ();
List<int> list1 = new();
// 有初始值
int[] numbers = new int[] { 1, 3, 2, 5, 4 };
List<int> list = numbers.ToList();

View File

@@ -31,7 +31,7 @@
=== "C#"
```csharp title="preorder_traversal_i_compact.cs"
[class]{preorder_traversal_i_compact}-[func]{preOrder}
[class]{preorder_traversal_i_compact}-[func]{PreOrder}
```
=== "Go"
@@ -119,7 +119,7 @@
=== "C#"
```csharp title="preorder_traversal_ii_compact.cs"
[class]{preorder_traversal_ii_compact}-[func]{preOrder}
[class]{preorder_traversal_ii_compact}-[func]{PreOrder}
```
=== "Go"
@@ -238,7 +238,7 @@
=== "C#"
```csharp title="preorder_traversal_iii_compact.cs"
[class]{preorder_traversal_iii_compact}-[func]{preOrder}
[class]{preorder_traversal_iii_compact}-[func]{PreOrder}
```
=== "Go"
@@ -377,23 +377,23 @@
```csharp title=""
/* 回溯算法框架 */
void backtrack(State state, List<Choice> choices, List<State> res) {
void Backtrack(State state, List<Choice> choices, List<State> res) {
// 判断是否为解
if (isSolution(state)) {
if (IsSolution(state)) {
// 记录解
recordSolution(state, res);
RecordSolution(state, res);
// 停止继续搜索
return;
}
// 遍历所有选择
foreach (Choice choice in choices) {
// 剪枝:判断选择是否合法
if (isValid(state, choice)) {
if (IsValid(state, choice)) {
// 尝试:做出选择,更新状态
makeChoice(state, choice);
backtrack(state, choices, res);
MakeChoice(state, choice);
Backtrack(state, choices, res);
// 回退:撤销选择,恢复到之前的状态
undoChoice(state, choice);
UndoChoice(state, choice);
}
}
}
@@ -640,17 +640,17 @@
=== "C#"
```csharp title="preorder_traversal_iii_template.cs"
[class]{preorder_traversal_iii_template}-[func]{isSolution}
[class]{preorder_traversal_iii_template}-[func]{IsSolution}
[class]{preorder_traversal_iii_template}-[func]{recordSolution}
[class]{preorder_traversal_iii_template}-[func]{RecordSolution}
[class]{preorder_traversal_iii_template}-[func]{isValid}
[class]{preorder_traversal_iii_template}-[func]{IsValid}
[class]{preorder_traversal_iii_template}-[func]{makeChoice}
[class]{preorder_traversal_iii_template}-[func]{MakeChoice}
[class]{preorder_traversal_iii_template}-[func]{undoChoice}
[class]{preorder_traversal_iii_template}-[func]{UndoChoice}
[class]{preorder_traversal_iii_template}-[func]{backtrack}
[class]{preorder_traversal_iii_template}-[func]{Backtrack}
```
=== "Go"

View File

@@ -67,9 +67,9 @@
=== "C#"
```csharp title="n_queens.cs"
[class]{n_queens}-[func]{backtrack}
[class]{n_queens}-[func]{Backtrack}
[class]{n_queens}-[func]{nQueens}
[class]{n_queens}-[func]{NQueens}
```
=== "Go"

View File

@@ -70,9 +70,9 @@
=== "C#"
```csharp title="permutations_i.cs"
[class]{permutations_i}-[func]{backtrack}
[class]{permutations_i}-[func]{Backtrack}
[class]{permutations_i}-[func]{permutationsI}
[class]{permutations_i}-[func]{PermutationsI}
```
=== "Go"
@@ -194,9 +194,9 @@
=== "C#"
```csharp title="permutations_ii.cs"
[class]{permutations_ii}-[func]{backtrack}
[class]{permutations_ii}-[func]{Backtrack}
[class]{permutations_ii}-[func]{permutationsII}
[class]{permutations_ii}-[func]{PermutationsII}
```
=== "Go"

View File

@@ -44,9 +44,9 @@
=== "C#"
```csharp title="subset_sum_i_naive.cs"
[class]{subset_sum_i_naive}-[func]{backtrack}
[class]{subset_sum_i_naive}-[func]{Backtrack}
[class]{subset_sum_i_naive}-[func]{subsetSumINaive}
[class]{subset_sum_i_naive}-[func]{SubsetSumINaive}
```
=== "Go"
@@ -177,9 +177,9 @@
=== "C#"
```csharp title="subset_sum_i.cs"
[class]{subset_sum_i}-[func]{backtrack}
[class]{subset_sum_i}-[func]{Backtrack}
[class]{subset_sum_i}-[func]{subsetSumI}
[class]{subset_sum_i}-[func]{SubsetSumI}
```
=== "Go"
@@ -297,9 +297,9 @@
=== "C#"
```csharp title="subset_sum_ii.cs"
[class]{subset_sum_ii}-[func]{backtrack}
[class]{subset_sum_ii}-[func]{Backtrack}
[class]{subset_sum_ii}-[func]{subsetSumII}
[class]{subset_sum_ii}-[func]{SubsetSumII}
```
=== "Go"

View File

@@ -33,7 +33,7 @@
=== "C#"
```csharp title="iteration.cs"
[class]{iteration}-[func]{forLoop}
[class]{iteration}-[func]{ForLoop}
```
=== "Go"
@@ -117,7 +117,7 @@
=== "C#"
```csharp title="iteration.cs"
[class]{iteration}-[func]{whileLoop}
[class]{iteration}-[func]{WhileLoop}
```
=== "Go"
@@ -193,7 +193,7 @@
=== "C#"
```csharp title="iteration.cs"
[class]{iteration}-[func]{whileLoopII}
[class]{iteration}-[func]{WhileLoopII}
```
=== "Go"
@@ -271,7 +271,7 @@
=== "C#"
```csharp title="iteration.cs"
[class]{iteration}-[func]{nestedForLoop}
[class]{iteration}-[func]{NestedForLoop}
```
=== "Go"
@@ -366,7 +366,7 @@
=== "C#"
```csharp title="recursion.cs"
[class]{recursion}-[func]{recur}
[class]{recursion}-[func]{Recur}
```
=== "Go"
@@ -474,7 +474,7 @@
=== "C#"
```csharp title="recursion.cs"
[class]{recursion}-[func]{tailRecur}
[class]{recursion}-[func]{TailRecur}
```
=== "Go"
@@ -572,7 +572,7 @@
=== "C#"
```csharp title="recursion.cs"
[class]{recursion}-[func]{fib}
[class]{recursion}-[func]{Fib}
```
=== "Go"
@@ -679,7 +679,7 @@
=== "C#"
```csharp title="recursion.cs"
[class]{recursion}-[func]{forLoopRecur}
[class]{recursion}-[func]{ForLoopRecur}
```
=== "Go"

View File

@@ -105,16 +105,16 @@
}
/* 函数 */
int function() {
int Function() {
// 执行某些操作...
return 0;
}
int algorithm(int n) { // 输入数据
int Algorithm(int n) { // 输入数据
const int a = 0; // 暂存数据(常量)
int b = 0; // 暂存数据(变量)
Node node = new Node(0); // 暂存数据(对象)
int c = function(); // 栈帧空间(调用函数)
Node node = new(0); // 暂存数据(对象)
int c = Function(); // 栈帧空间(调用函数)
return a + b + c; // 输出数据
}
```
@@ -360,7 +360,7 @@
=== "C#"
```csharp title=""
void algorithm(int n) {
void Algorithm(int n) {
int a = 0; // O(1)
int[] b = new int[10000]; // O(1)
if (n > 10) {
@@ -526,20 +526,20 @@
=== "C#"
```csharp title=""
int function() {
int Function() {
// 执行某些操作
return 0;
}
/* 循环 O(1) */
void loop(int n) {
void Loop(int n) {
for (int i = 0; i < n; i++) {
function();
Function();
}
}
/* 递归 O(n) */
int recur(int n) {
int Recur(int n) {
if (n == 1) return 1;
return recur(n - 1);
return Recur(n - 1);
}
```
@@ -746,9 +746,9 @@ $$
=== "C#"
```csharp title="space_complexity.cs"
[class]{space_complexity}-[func]{function}
[class]{space_complexity}-[func]{Function}
[class]{space_complexity}-[func]{constant}
[class]{space_complexity}-[func]{Constant}
```
=== "Go"
@@ -840,7 +840,7 @@ $$
=== "C#"
```csharp title="space_complexity.cs"
[class]{space_complexity}-[func]{linear}
[class]{space_complexity}-[func]{Linear}
```
=== "Go"
@@ -916,7 +916,7 @@ $$
=== "C#"
```csharp title="space_complexity.cs"
[class]{space_complexity}-[func]{linearRecur}
[class]{space_complexity}-[func]{LinearRecur}
```
=== "Go"
@@ -994,7 +994,7 @@ $$
=== "C#"
```csharp title="space_complexity.cs"
[class]{space_complexity}-[func]{quadratic}
[class]{space_complexity}-[func]{Quadratic}
```
=== "Go"
@@ -1068,7 +1068,7 @@ $$
=== "C#"
```csharp title="space_complexity.cs"
[class]{space_complexity}-[func]{quadraticRecur}
[class]{space_complexity}-[func]{QuadraticRecur}
```
=== "Go"
@@ -1146,7 +1146,7 @@ $$
=== "C#"
```csharp title="space_complexity.cs"
[class]{space_complexity}-[func]{buildTree}
[class]{space_complexity}-[func]{BuildTree}
```
=== "Go"

View File

@@ -55,7 +55,7 @@
```csharp title=""
// 在某运行平台下
void algorithm(int n) {
void Algorithm(int n) {
int a = 2; // 1 ns
a = a + 1; // 1 ns
a = a * 2; // 10 ns
@@ -253,17 +253,17 @@ $$
```csharp title=""
// 算法 A 的时间复杂度:常数阶
void algorithm_A(int n) {
void AlgorithmA(int n) {
Console.WriteLine(0);
}
// 算法 B 的时间复杂度:线性阶
void algorithm_B(int n) {
void AlgorithmB(int n) {
for (int i = 0; i < n; i++) {
Console.WriteLine(0);
}
}
// 算法 C 的时间复杂度:常数阶
void algorithm_C(int n) {
void AlgorithmC(int n) {
for (int i = 0; i < 1000000; i++) {
Console.WriteLine(0);
}
@@ -487,7 +487,7 @@ $$
=== "C#"
```csharp title=""
void algorithm(int n) {
void Algorithm(int n) {
int a = 1; // +1
a = a + 1; // +1
a = a * 2; // +1
@@ -695,7 +695,7 @@ $T(n)$ 是一次函数,说明其运行时间的增长趋势是线性的,因
=== "C#"
```csharp title=""
void algorithm(int n) {
void Algorithm(int n) {
int a = 1; // +0技巧 1
a = a + n; // +0技巧 1
// +n技巧 2
@@ -918,7 +918,7 @@ $$
=== "C#"
```csharp title="time_complexity.cs"
[class]{time_complexity}-[func]{constant}
[class]{time_complexity}-[func]{Constant}
```
=== "Go"
@@ -994,7 +994,7 @@ $$
=== "C#"
```csharp title="time_complexity.cs"
[class]{time_complexity}-[func]{linear}
[class]{time_complexity}-[func]{Linear}
```
=== "Go"
@@ -1068,7 +1068,7 @@ $$
=== "C#"
```csharp title="time_complexity.cs"
[class]{time_complexity}-[func]{arrayTraversal}
[class]{time_complexity}-[func]{ArrayTraversal}
```
=== "Go"
@@ -1146,7 +1146,7 @@ $$
=== "C#"
```csharp title="time_complexity.cs"
[class]{time_complexity}-[func]{quadratic}
[class]{time_complexity}-[func]{Quadratic}
```
=== "Go"
@@ -1224,7 +1224,7 @@ $$
=== "C#"
```csharp title="time_complexity.cs"
[class]{time_complexity}-[func]{bubbleSort}
[class]{time_complexity}-[func]{BubbleSort}
```
=== "Go"
@@ -1302,7 +1302,7 @@ $$
=== "C#"
```csharp title="time_complexity.cs"
[class]{time_complexity}-[func]{exponential}
[class]{time_complexity}-[func]{Exponential}
```
=== "Go"
@@ -1378,7 +1378,7 @@ $$
=== "C#"
```csharp title="time_complexity.cs"
[class]{time_complexity}-[func]{expRecur}
[class]{time_complexity}-[func]{ExpRecur}
```
=== "Go"
@@ -1458,7 +1458,7 @@ $$
=== "C#"
```csharp title="time_complexity.cs"
[class]{time_complexity}-[func]{logarithmic}
[class]{time_complexity}-[func]{Logarithmic}
```
=== "Go"
@@ -1534,7 +1534,7 @@ $$
=== "C#"
```csharp title="time_complexity.cs"
[class]{time_complexity}-[func]{logRecur}
[class]{time_complexity}-[func]{LogRecur}
```
=== "Go"
@@ -1622,7 +1622,7 @@ $$
=== "C#"
```csharp title="time_complexity.cs"
[class]{time_complexity}-[func]{linearLogRecur}
[class]{time_complexity}-[func]{LinearLogRecur}
```
=== "Go"
@@ -1710,7 +1710,7 @@ $$
=== "C#"
```csharp title="time_complexity.cs"
[class]{time_complexity}-[func]{factorialRecur}
[class]{time_complexity}-[func]{FactorialRecur}
```
=== "Go"
@@ -1801,9 +1801,9 @@ $$
=== "C#"
```csharp title="worst_best_time_complexity.cs"
[class]{worst_best_time_complexity}-[func]{randomNumbers}
[class]{worst_best_time_complexity}-[func]{RandomNumbers}
[class]{worst_best_time_complexity}-[func]{findOne}
[class]{worst_best_time_complexity}-[func]{FindOne}
```
=== "Go"

View File

@@ -67,9 +67,9 @@
=== "C#"
```csharp title="binary_search_recur.cs"
[class]{binary_search_recur}-[func]{dfs}
[class]{binary_search_recur}-[func]{Dfs}
[class]{binary_search_recur}-[func]{binarySearch}
[class]{binary_search_recur}-[func]{BinarySearch}
```
=== "Go"

View File

@@ -84,9 +84,9 @@
=== "C#"
```csharp title="build_tree.cs"
[class]{build_tree}-[func]{dfs}
[class]{build_tree}-[func]{Dfs}
[class]{build_tree}-[func]{buildTree}
[class]{build_tree}-[func]{BuildTree}
```
=== "Go"

View File

@@ -115,11 +115,11 @@
=== "C#"
```csharp title="hanota.cs"
[class]{hanota}-[func]{move}
[class]{hanota}-[func]{Move}
[class]{hanota}-[func]{dfs}
[class]{hanota}-[func]{Dfs}
[class]{hanota}-[func]{solveHanota}
[class]{hanota}-[func]{SolveHanota}
```
=== "Go"

View File

@@ -55,7 +55,7 @@ $$
=== "C#"
```csharp title="min_cost_climbing_stairs_dp.cs"
[class]{min_cost_climbing_stairs_dp}-[func]{minCostClimbingStairsDP}
[class]{min_cost_climbing_stairs_dp}-[func]{MinCostClimbingStairsDP}
```
=== "Go"
@@ -133,7 +133,7 @@ $$
=== "C#"
```csharp title="min_cost_climbing_stairs_dp.cs"
[class]{min_cost_climbing_stairs_dp}-[func]{minCostClimbingStairsDPComp}
[class]{min_cost_climbing_stairs_dp}-[func]{MinCostClimbingStairsDPComp}
```
=== "Go"
@@ -243,7 +243,7 @@ $$
=== "C#"
```csharp title="climbing_stairs_constraint_dp.cs"
[class]{climbing_stairs_constraint_dp}-[func]{climbingStairsConstraintDP}
[class]{climbing_stairs_constraint_dp}-[func]{ClimbingStairsConstraintDP}
```
=== "Go"

View File

@@ -119,7 +119,7 @@ $$
=== "C#"
```csharp title="min_path_sum.cs"
[class]{min_path_sum}-[func]{minPathSumDFS}
[class]{min_path_sum}-[func]{MinPathSumDFS}
```
=== "Go"
@@ -203,7 +203,7 @@ $$
=== "C#"
```csharp title="min_path_sum.cs"
[class]{min_path_sum}-[func]{minPathSumDFSMem}
[class]{min_path_sum}-[func]{MinPathSumDFSMem}
```
=== "Go"
@@ -283,7 +283,7 @@ $$
=== "C#"
```csharp title="min_path_sum.cs"
[class]{min_path_sum}-[func]{minPathSumDP}
[class]{min_path_sum}-[func]{MinPathSumDP}
```
=== "Go"
@@ -401,7 +401,7 @@ $$
=== "C#"
```csharp title="min_path_sum.cs"
[class]{min_path_sum}-[func]{minPathSumDPComp}
[class]{min_path_sum}-[func]{MinPathSumDPComp}
```
=== "Go"

View File

@@ -88,7 +88,7 @@ $$
=== "C#"
```csharp title="edit_distance.cs"
[class]{edit_distance}-[func]{editDistanceDP}
[class]{edit_distance}-[func]{EditDistanceDP}
```
=== "Go"
@@ -213,7 +213,7 @@ $$
=== "C#"
```csharp title="edit_distance.cs"
[class]{edit_distance}-[func]{editDistanceDPComp}
[class]{edit_distance}-[func]{EditDistanceDPComp}
```
=== "Go"

View File

@@ -41,9 +41,9 @@
=== "C#"
```csharp title="climbing_stairs_backtrack.cs"
[class]{climbing_stairs_backtrack}-[func]{backtrack}
[class]{climbing_stairs_backtrack}-[func]{Backtrack}
[class]{climbing_stairs_backtrack}-[func]{climbingStairsBacktrack}
[class]{climbing_stairs_backtrack}-[func]{ClimbingStairsBacktrack}
```
=== "Go"
@@ -163,9 +163,9 @@ $$
=== "C#"
```csharp title="climbing_stairs_dfs.cs"
[class]{climbing_stairs_dfs}-[func]{dfs}
[class]{climbing_stairs_dfs}-[func]{Dfs}
[class]{climbing_stairs_dfs}-[func]{climbingStairsDFS}
[class]{climbing_stairs_dfs}-[func]{ClimbingStairsDFS}
```
=== "Go"
@@ -274,9 +274,9 @@ $$
=== "C#"
```csharp title="climbing_stairs_dfs_mem.cs"
[class]{climbing_stairs_dfs_mem}-[func]{dfs}
[class]{climbing_stairs_dfs_mem}-[func]{Dfs}
[class]{climbing_stairs_dfs_mem}-[func]{climbingStairsDFSMem}
[class]{climbing_stairs_dfs_mem}-[func]{ClimbingStairsDFSMem}
```
=== "Go"
@@ -376,7 +376,7 @@ $$
=== "C#"
```csharp title="climbing_stairs_dp.cs"
[class]{climbing_stairs_dp}-[func]{climbingStairsDP}
[class]{climbing_stairs_dp}-[func]{ClimbingStairsDP}
```
=== "Go"
@@ -464,7 +464,7 @@ $$
=== "C#"
```csharp title="climbing_stairs_dp.cs"
[class]{climbing_stairs_dp}-[func]{climbingStairsDPComp}
[class]{climbing_stairs_dp}-[func]{ClimbingStairsDPComp}
```
=== "Go"

View File

@@ -77,7 +77,7 @@ $$
=== "C#"
```csharp title="knapsack.cs"
[class]{knapsack}-[func]{knapsackDFS}
[class]{knapsack}-[func]{KnapsackDFS}
```
=== "Go"
@@ -161,7 +161,7 @@ $$
=== "C#"
```csharp title="knapsack.cs"
[class]{knapsack}-[func]{knapsackDFSMem}
[class]{knapsack}-[func]{KnapsackDFSMem}
```
=== "Go"
@@ -241,7 +241,7 @@ $$
=== "C#"
```csharp title="knapsack.cs"
[class]{knapsack}-[func]{knapsackDP}
[class]{knapsack}-[func]{KnapsackDP}
```
=== "Go"
@@ -388,7 +388,7 @@ $$
=== "C#"
```csharp title="knapsack.cs"
[class]{knapsack}-[func]{knapsackDPComp}
[class]{knapsack}-[func]{KnapsackDPComp}
```
=== "Go"

Some files were not shown because too many files have changed in this diff Show More