Add build scripts for C# and

unify the coding style.
This commit is contained in:
krahets 2023-02-08 22:18:02 +08:00
parent 38751cc5f5
commit 6dc21691ed
63 changed files with 2703 additions and 3911 deletions

View file

@ -72,11 +72,10 @@ public:
/* 将数组转化为 Vector 并返回 */ /* 将数组转化为 Vector 并返回 */
vector<int> toVector() { vector<int> toVector() {
int cap = queCapacity;
// 仅转换有效长度范围内的列表元素 // 仅转换有效长度范围内的列表元素
vector<int> arr(queSize); vector<int> arr(queSize);
for (int i = 0, j = front; i < queSize; i++, j++) { for (int i = 0, j = front; i < queSize; i++, j++) {
arr[i] = nums[j % cap]; arr[i] = nums[j % queCapacity];
} }
return arr; return arr;
} }

View file

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

View file

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

View file

@ -6,70 +6,69 @@
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_array_and_linkedlist namespace hello_algo.chapter_array_and_linkedlist;
public class list
{ {
public class list [Test]
public void Test()
{ {
[Test]
public void Test() /* 初始化列表 */
// 注意数组的元素类型是 int[] 的包装类 int[]
int[] numbers = new int[] { 1, 3, 2, 5, 4 };
List<int> list = numbers.ToList();
Console.WriteLine("列表 list = " + string.Join(",", list));
/* 访问元素 */
int num = list[1];
Console.WriteLine("访问索引 1 处的元素,得到 num = " + num);
/* 更新元素 */
list[1] = 0;
Console.WriteLine("将索引 1 处的元素更新为 0 ,得到 list = " + string.Join(",", list));
/* 清空列表 */
list.Clear();
Console.WriteLine("清空列表后 list = " + string.Join(",", list));
/* 尾部添加元素 */
list.Add(1);
list.Add(3);
list.Add(2);
list.Add(5);
list.Add(4);
Console.WriteLine("添加元素后 list = " + string.Join(",", list));
/* 中间插入元素 */
list.Insert(3, 6);
Console.WriteLine("在索引 3 处插入数字 6 ,得到 list = " + string.Join(",", list));
/* 删除元素 */
list.RemoveAt(3);
Console.WriteLine("删除索引 3 处的元素,得到 list = " + string.Join(",", list));
/* 通过索引遍历列表 */
int count = 0;
for (int i = 0; i < list.Count(); i++)
{ {
count++;
/* 初始化列表 */
// 注意数组的元素类型是 int[] 的包装类 int[]
int[] numbers = new int[] { 1, 3, 2, 5, 4 };
List<int> list = numbers.ToList();
Console.WriteLine("列表 list = " + string.Join(",",list));
/* 访问元素 */
int num = list[1];
Console.WriteLine("访问索引 1 处的元素,得到 num = " + num);
/* 更新元素 */
list[1] = 0;
Console.WriteLine("将索引 1 处的元素更新为 0 ,得到 list = " + string.Join(",", list));
/* 清空列表 */
list.Clear();
Console.WriteLine("清空列表后 list = " + string.Join(",", list));
/* 尾部添加元素 */
list.Add(1);
list.Add(3);
list.Add(2);
list.Add(5);
list.Add(4);
Console.WriteLine("添加元素后 list = " + string.Join(",", list));
/* 中间插入元素 */
list.Insert(3, 6);
Console.WriteLine("在索引 3 处插入数字 6 ,得到 list = " + string.Join(",", list));
/* 删除元素 */
list.RemoveAt(3);
Console.WriteLine("删除索引 3 处的元素,得到 list = " + string.Join(",", list));
/* 通过索引遍历列表 */
int count = 0;
for (int i = 0; i < list.Count(); i++)
{
count++;
}
/* 直接遍历列表元素 */
count = 0;
foreach (int n in list)
{
count++;
}
/* 拼接两个列表 */
List<int> list1 = new() { 6, 8, 7, 10, 9 };
list.AddRange(list1);
Console.WriteLine("将列表 list1 拼接到 list 之后,得到 list = " + string.Join(",", list));
/* 排序列表 */
list.Sort(); // 排序后列表元素从小到大排列
Console.WriteLine("排序列表后 list = " + string.Join(",", list));
} }
/* 直接遍历列表元素 */
count = 0;
foreach (int n in list)
{
count++;
}
/* 拼接两个列表 */
List<int> list1 = new() { 6, 8, 7, 10, 9 };
list.AddRange(list1);
Console.WriteLine("将列表 list1 拼接到 list 之后,得到 list = " + string.Join(",", list));
/* 排序列表 */
list.Sort(); // 排序后列表元素从小到大排列
Console.WriteLine("排序列表后 list = " + string.Join(",", list));
} }
} }

View file

@ -6,159 +6,158 @@
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_array_and_linkedlist namespace hello_algo.chapter_array_and_linkedlist;
/* 列表类简易实现 */
class MyList
{ {
class MyList private int[] nums; // 数组存储列表元素
private int numsCapacity = 10; // 列表容量
private int numsSize = 0; // 列表长度即当前元素数量
private int extendRatio = 2; // 每次列表扩容的倍数
/* 构造函数 */
public MyList()
{ {
private int[] nums; // 数组存储列表元素 nums = new int[numsCapacity];
private int capacity = 10; // 列表容量
private int size = 0; // 列表长度即当前元素数量
private int extendRatio = 2; // 每次列表扩容的倍数
/* 构造函数 */
public MyList()
{
nums = new int[capacity];
}
/* 获取列表长度(即当前元素数量)*/
public int Size()
{
return size;
}
/* 获取列表容量 */
public int Capacity()
{
return capacity;
}
/* 访问元素 */
public int Get(int index)
{
// 索引如果越界则抛出异常下同
if (index < 0 || index >= size)
throw new IndexOutOfRangeException("索引越界");
return nums[index];
}
/* 更新元素 */
public void Set(int index, int num)
{
if (index < 0 || index >= size)
throw new IndexOutOfRangeException("索引越界");
nums[index] = num;
}
/* 尾部添加元素 */
public void Add(int num)
{
// 元素数量超出容量时触发扩容机制
if (size == Capacity())
ExtendCapacity();
nums[size] = num;
// 更新元素数量
size++;
}
/* 中间插入元素 */
public void Insert(int index, int num)
{
if (index < 0 || index >= size)
throw new IndexOutOfRangeException("索引越界");
// 元素数量超出容量时触发扩容机制
if (size == Capacity())
ExtendCapacity();
// 将索引 index 以及之后的元素都向后移动一位
for (int j = size - 1; j >= index; j--)
{
nums[j + 1] = nums[j];
}
nums[index] = num;
// 更新元素数量
size++;
}
/* 删除元素 */
public int Remove(int index)
{
if (index < 0 || index >= size)
throw new IndexOutOfRangeException("索引越界");
int num = nums[index];
// 将索引 index 之后的元素都向前移动一位
for (int j = index; j < size - 1; j++)
{
nums[j] = nums[j + 1];
}
// 更新元素数量
size--;
// 返回被删除元素
return num;
}
/* 列表扩容 */
public void ExtendCapacity()
{
// 新建一个长度为 size 的数组并将原数组拷贝到新数组
System.Array.Resize(ref nums, Capacity() * extendRatio);
// 更新列表容量
capacity = nums.Length;
}
/* 将列表转换为数组 */
public int[] ToArray()
{
int size = Size();
// 仅转换有效长度范围内的列表元素
int[] nums = new int[size];
for (int i = 0; i < size; i++)
{
nums[i] = Get(i);
}
return nums;
}
} }
public class my_list /* 获取列表长度(即当前元素数量)*/
public int size()
{ {
[Test] return numsSize;
public void Test() }
/* 获取列表容量 */
public int capacity()
{
return numsCapacity;
}
/* 访问元素 */
public int get(int index)
{
// 索引如果越界则抛出异常下同
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
return nums[index];
}
/* 更新元素 */
public void set(int index, int num)
{
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
nums[index] = num;
}
/* 尾部添加元素 */
public void add(int num)
{
// 元素数量超出容量时触发扩容机制
if (numsSize == numsCapacity)
extendCapacity();
nums[numsSize] = num;
// 更新元素数量
numsSize++;
}
/* 中间插入元素 */
public void insert(int index, int num)
{
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
// 元素数量超出容量时触发扩容机制
if (numsSize == numsCapacity)
extendCapacity();
// 将索引 index 以及之后的元素都向后移动一位
for (int j = numsSize - 1; j >= index; j--)
{ {
/* 初始化列表 */ nums[j + 1] = nums[j];
MyList list = new MyList();
/* 尾部添加元素 */
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.Remove(3);
Console.WriteLine("删除索引 3 处的元素,得到 list = " + string.Join(",", list.ToArray()));
/* 访问元素 */
int num = list.Get(1);
Console.WriteLine("访问索引 1 处的元素,得到 num = " + num);
/* 更新元素 */
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);
}
Console.WriteLine("扩容后的列表 list = " + string.Join(",", list.ToArray()) +
" ,容量 = " + list.Capacity() + " ,长度 = " + list.Size());
} }
nums[index] = num;
// 更新元素数量
numsSize++;
}
/* 删除元素 */
public int remove(int index)
{
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
int num = nums[index];
// 将索引 index 之后的元素都向前移动一位
for (int j = index; j < numsSize - 1; j++)
{
nums[j] = nums[j + 1];
}
// 更新元素数量
numsSize--;
// 返回被删除元素
return num;
}
/* 列表扩容 */
public void extendCapacity()
{
// 新建一个长度为 numsCapacity * extendRatio 的数组并将原数组拷贝到新数组
System.Array.Resize(ref nums, numsCapacity * extendRatio);
// 更新列表容量
numsCapacity = nums.Length;
}
/* 将列表转换为数组 */
public int[] toArray()
{
// 仅转换有效长度范围内的列表元素
int[] nums = new int[numsSize];
for (int i = 0; i < numsSize; i++)
{
nums[i] = get(i);
}
return nums;
}
}
public class my_list
{
[Test]
public void Test()
{
/* 初始化列表 */
MyList list = new MyList();
/* 尾部添加元素 */
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.remove(3);
Console.WriteLine("删除索引 3 处的元素,得到 list = " + string.Join(",", list.toArray()));
/* 访问元素 */
int num = list.get(1);
Console.WriteLine("访问索引 1 处的元素,得到 num = " + num);
/* 更新元素 */
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);
}
Console.WriteLine("扩容后的列表 list = " + string.Join(",", list.toArray()) +
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
} }
} }

View file

@ -6,58 +6,57 @@
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_computational_complexity namespace hello_algo.chapter_computational_complexity;
public class leetcode_two_sum
{ {
public class leetcode_two_sum /* 方法一:暴力枚举 */
public static int[] twoSumBruteForce(int[] nums, int target)
{ {
/* 方法一:暴力枚举 */ int size = nums.Length;
public static int[] twoSumBruteForce(int[] nums, int target) // 两层循环时间复杂度 O(n^2)
for (int i = 0; i < size - 1; i++)
{ {
int size = nums.Length; for (int j = i + 1; j < size; j++)
// 两层循环时间复杂度 O(n^2)
for (int i = 0; i < size - 1; i++)
{ {
for (int j = i + 1; j < size; j++) if (nums[i] + nums[j] == target)
{ return new int[] { i, j };
if (nums[i] + nums[j] == target)
return new int[] { i, j };
}
} }
return new int[0];
}
/* 方法二:辅助哈希表 */
public static int[] twoSumHashTable(int[] nums, int target)
{
int size = nums.Length;
// 辅助哈希表空间复杂度 O(n)
Dictionary<int, int> dic = new();
// 单层循环时间复杂度 O(n)
for (int i = 0; i < size; i++)
{
if (dic.ContainsKey(target - nums[i]))
{
return new int[] { dic[target - nums[i]], i };
}
dic.Add(nums[i], i);
}
return new int[0];
}
[Test]
public void Test()
{
// ======= Test Case =======
int[] nums = { 2, 7, 11, 15 };
int target = 9;
// ====== Driver Code ======
// 方法一
int[] res = twoSumBruteForce(nums, target);
Console.WriteLine("方法一 res = " + string.Join(",", res));
// 方法二
res = twoSumHashTable(nums, target);
Console.WriteLine("方法二 res = " + string.Join(",", res));
} }
return new int[0];
} }
}
/* 方法二:辅助哈希表 */
public static int[] twoSumHashTable(int[] nums, int target)
{
int size = nums.Length;
// 辅助哈希表空间复杂度 O(n)
Dictionary<int, int> dic = new();
// 单层循环时间复杂度 O(n)
for (int i = 0; i < size; i++)
{
if (dic.ContainsKey(target - nums[i]))
{
return new int[] { dic[target - nums[i]], i };
}
dic.Add(nums[i], i);
}
return new int[0];
}
[Test]
public void Test()
{
// ======= Test Case =======
int[] nums = { 2, 7, 11, 15 };
int target = 9;
// ====== Driver Code ======
// 方法一
int[] res = twoSumBruteForce(nums, target);
Console.WriteLine("方法一 res = " + string.Join(",", res));
// 方法二
res = twoSumHashTable(nums, target);
Console.WriteLine("方法二 res = " + string.Join(",", res));
}
}

View file

@ -7,116 +7,115 @@
using hello_algo.include; using hello_algo.include;
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_computational_complexity namespace hello_algo.chapter_computational_complexity;
public class space_complexity
{ {
public class space_complexity /* 函数 */
static int function()
{ {
/* 函数 */ // do something
static int function() return 0;
{ }
// do something
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);
// 循环中的变量占用 O(1) 空间
for (int i = 0; i < n; i++)
{ {
// 常量变量对象占用 O(1) 空间 int c = 0;
int a = 0;
int b = 0;
int[] nums = new int[10000];
ListNode node = new ListNode(0);
// 循环中的变量占用 O(1) 空间
for (int i = 0; i < n; i++)
{
int c = 0;
}
// 循环中的函数占用 O(1) 空间
for (int i = 0; i < n; i++)
{
function();
}
} }
// 循环中的函数占用 O(1) 空间
/* 线性阶 */ for (int i = 0; i < n; i++)
static void linear(int n)
{ {
// 长度为 n 的数组占用 O(n) 空间 function();
int[] nums = new int[n];
// 长度为 n 的列表占用 O(n) 空间
List<ListNode> nodes = new();
for (int i = 0; i < n; i++)
{
nodes.Add(new ListNode(i));
}
// 长度为 n 的哈希表占用 O(n) 空间
Dictionary<int, String> map = new();
for (int i = 0; i < n; i++)
{
map.Add(i, i.ToString());
}
}
/* 线性阶(递归实现) */
static void linearRecur(int n)
{
Console.WriteLine("递归 n = " + n);
if (n == 1) return;
linearRecur(n - 1);
}
/* 平方阶 */
static void quadratic(int n)
{
// 矩阵占用 O(n^2) 空间
int[,] numMatrix = new int[n, n];
// 二维列表占用 O(n^2) 空间
List<List<int>> numList = new();
for (int i = 0; i < n; i++)
{
List<int> tmp = new();
for (int j = 0; j < n; j++)
{
tmp.Add(0);
}
numList.Add(tmp);
}
}
/* 平方阶(递归实现) */
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);
}
/* 指数阶(建立满二叉树) */
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);
return root;
}
[Test]
public void Test()
{
int n = 5;
// 常数阶
constant(n);
// 线性阶
linear(n);
linearRecur(n);
// 平方阶
quadratic(n);
quadraticRecur(n);
// 指数阶
TreeNode? root = buildTree(n);
PrintUtil.PrintTree(root);
} }
} }
/* 线性阶 */
static void linear(int n)
{
// 长度为 n 的数组占用 O(n) 空间
int[] nums = new int[n];
// 长度为 n 的列表占用 O(n) 空间
List<ListNode> nodes = new();
for (int i = 0; i < n; i++)
{
nodes.Add(new ListNode(i));
}
// 长度为 n 的哈希表占用 O(n) 空间
Dictionary<int, String> map = new();
for (int i = 0; i < n; i++)
{
map.Add(i, i.ToString());
}
}
/* 线性阶(递归实现) */
static void linearRecur(int n)
{
Console.WriteLine("递归 n = " + n);
if (n == 1) return;
linearRecur(n - 1);
}
/* 平方阶 */
static void quadratic(int n)
{
// 矩阵占用 O(n^2) 空间
int[,] numMatrix = new int[n, n];
// 二维列表占用 O(n^2) 空间
List<List<int>> numList = new();
for (int i = 0; i < n; i++)
{
List<int> tmp = new();
for (int j = 0; j < n; j++)
{
tmp.Add(0);
}
numList.Add(tmp);
}
}
/* 平方阶(递归实现) */
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);
}
/* 指数阶(建立满二叉树) */
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);
return root;
}
[Test]
public void Test()
{
int n = 5;
// 常数阶
constant(n);
// 线性阶
linear(n);
linearRecur(n);
// 平方阶
quadratic(n);
quadraticRecur(n);
// 指数阶
TreeNode? root = buildTree(n);
PrintUtil.PrintTree(root);
}
} }

View file

@ -6,227 +6,226 @@
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_computational_complexity namespace hello_algo.chapter_computational_complexity;
{
public class time_complexity
{
void algorithm(int n)
{
int a = 1; // +0技巧 1
a = a + n; // +0技巧 1
// +n技巧 2
for (int i = 0; i < 5 * n + 1; i++)
{
Console.WriteLine(0);
}
// +n*n技巧 3
for (int i = 0; i < 2 * n; i++)
{
for (int j = 0; j < n + 1; j++)
{
Console.WriteLine(0);
}
}
}
// 算法 A 时间复杂度常数阶 public class time_complexity
void algorithm_A(int n) {
void algorithm(int n)
{
int a = 1; // +0技巧 1
a = a + n; // +0技巧 1
// +n技巧 2
for (int i = 0; i < 5 * n + 1; i++)
{ {
Console.WriteLine(0); Console.WriteLine(0);
} }
// 算法 B 时间复杂度线性阶 // +n*n技巧 3
void algorithm_B(int n) for (int i = 0; i < 2 * n; i++)
{ {
for (int i = 0; i < n; i++) for (int j = 0; j < n + 1; j++)
{ {
Console.WriteLine(0); Console.WriteLine(0);
} }
} }
// 算法 C 时间复杂度常数阶 }
void algorithm_C(int n)
{ // 算法 A 时间复杂度常数阶
for (int i = 0; i < 1000000; i++) void algorithm_A(int n)
{ {
Console.WriteLine(0); Console.WriteLine(0);
} }
} // 算法 B 时间复杂度线性阶
void algorithm_B(int n)
/* 常数阶 */ {
static int constant(int n) for (int i = 0; i < n; i++)
{ {
int count = 0; Console.WriteLine(0);
int size = 100000; }
for (int i = 0; i < size; i++) }
count++; // 算法 C 时间复杂度常数阶
return count; void algorithm_C(int n)
} {
for (int i = 0; i < 1000000; i++)
/* 线性阶 */ {
static int linear(int n) Console.WriteLine(0);
{ }
int count = 0; }
for (int i = 0; i < n; i++)
count++; /* 常数阶 */
return count; static int constant(int n)
} {
int count = 0;
/* 线性阶(遍历数组) */ int size = 100000;
static int arrayTraversal(int[] nums) for (int i = 0; i < size; i++)
{ count++;
int count = 0; return count;
// 循环次数与数组长度成正比 }
foreach (int num in nums)
{ /* 线性阶 */
count++; static int linear(int n)
} {
return count; int count = 0;
} for (int i = 0; i < n; i++)
count++;
/* 平方阶 */ return count;
static int quadratic(int n) }
{
int count = 0; /* 线性阶(遍历数组) */
// 循环次数与数组长度成平方关系 static int arrayTraversal(int[] nums)
for (int i = 0; i < n; i++) {
{ int count = 0;
for (int j = 0; j < n; j++) // 循环次数与数组长度成正比
{ foreach (int num in nums)
count++; {
} count++;
} }
return count; return count;
} }
/* 平方阶(冒泡排序) */ /* 平方阶 */
static int bubbleSort(int[] nums) static int quadratic(int n)
{ {
int count = 0; // 计数器 int count = 0;
// 外循环待排序元素数量为 n-1, n-2, ..., 1 // 循环次数与数组长度成平方关系
for (int i = nums.Length - 1; i > 0; i--) for (int i = 0; i < n; i++)
{ {
// 内循环冒泡操作 for (int j = 0; j < n; j++)
for (int j = 0; j < i; j++) {
{ count++;
if (nums[j] > nums[j + 1]) }
{ }
// 交换 nums[j] nums[j + 1] return count;
int tmp = nums[j]; }
nums[j] = nums[j + 1];
nums[j + 1] = tmp; /* 平方阶(冒泡排序) */
count += 3; // 元素交换包含 3 个单元操作 static int bubbleSort(int[] nums)
} {
} int count = 0; // 计数器
} // 外循环待排序元素数量为 n-1, n-2, ..., 1
return count; for (int i = nums.Length - 1; i > 0; i--)
} {
// 内循环冒泡操作
/* 指数阶(循环实现) */ for (int j = 0; j < i; j++)
static int exponential(int n) {
{ if (nums[j] > nums[j + 1])
int count = 0, bas = 1; {
// cell 每轮一分为二形成数列 1, 2, 4, 8, ..., 2^(n-1) // 交换 nums[j] nums[j + 1]
for (int i = 0; i < n; i++) int tmp = nums[j];
{ nums[j] = nums[j + 1];
for (int j = 0; j < bas; j++) nums[j + 1] = tmp;
{ count += 3; // 元素交换包含 3 个单元操作
count++; }
} }
bas *= 2; }
} return count;
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1 }
return count;
} /* 指数阶(循环实现) */
static int exponential(int n)
/* 指数阶(递归实现) */ {
static int expRecur(int n) int count = 0, bas = 1;
{ // cell 每轮一分为二形成数列 1, 2, 4, 8, ..., 2^(n-1)
if (n == 1) return 1; for (int i = 0; i < n; i++)
return expRecur(n - 1) + expRecur(n - 1) + 1; {
} for (int j = 0; j < bas; j++)
{
/* 对数阶(循环实现) */ count++;
static int logarithmic(float n) }
{ bas *= 2;
int count = 0; }
while (n > 1) // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
{ return count;
n = n / 2; }
count++;
} /* 指数阶(递归实现) */
return count; static int expRecur(int n)
} {
if (n == 1) return 1;
/* 对数阶(递归实现) */ return expRecur(n - 1) + expRecur(n - 1) + 1;
static int logRecur(float n) }
{
if (n <= 1) return 0; /* 对数阶(循环实现) */
return logRecur(n / 2) + 1; static int logarithmic(float n)
} {
int count = 0;
/* 线性对数阶 */ while (n > 1)
static int linearLogRecur(float n) {
{ n = n / 2;
if (n <= 1) return 1; count++;
int count = linearLogRecur(n / 2) + }
linearLogRecur(n / 2); return count;
for (int i = 0; i < n; i++) }
{
count++; /* 对数阶(递归实现) */
} static int logRecur(float n)
return count; {
} if (n <= 1) return 0;
return logRecur(n / 2) + 1;
/* 阶乘阶(递归实现) */ }
static int factorialRecur(int n)
{ /* 线性对数阶 */
if (n == 0) return 1; static int linearLogRecur(float n)
int count = 0; {
// 1 个分裂出 n if (n <= 1) return 1;
for (int i = 0; i < n; i++) int count = linearLogRecur(n / 2) +
{ linearLogRecur(n / 2);
count += factorialRecur(n - 1); for (int i = 0; i < n; i++)
} {
return count; count++;
} }
return count;
[Test] }
public void Test()
{ /* 阶乘阶(递归实现) */
// 可以修改 n 运行体会一下各种复杂度的操作数量变化趋势 static int factorialRecur(int n)
int n = 8; {
Console.WriteLine("输入数据大小 n = " + n); if (n == 0) return 1;
int count = 0;
int count = constant(n); // 1 个分裂出 n
Console.WriteLine("常数阶的计算操作数量 = " + count); for (int i = 0; i < n; i++)
{
count = linear(n); count += factorialRecur(n - 1);
Console.WriteLine("线性阶的计算操作数量 = " + count); }
count = arrayTraversal(new int[n]); return count;
Console.WriteLine("线性阶(遍历数组)的计算操作数量 = " + count); }
count = quadratic(n); [Test]
Console.WriteLine("平方阶的计算操作数量 = " + count); public void Test()
int[] nums = new int[n]; {
for (int i = 0; i < n; i++) // 可以修改 n 运行体会一下各种复杂度的操作数量变化趋势
nums[i] = n - i; // [n,n-1,...,2,1] int n = 8;
count = bubbleSort(nums); Console.WriteLine("输入数据大小 n = " + n);
Console.WriteLine("平方阶(冒泡排序)的计算操作数量 = " + count);
int count = constant(n);
count = exponential(n); Console.WriteLine("常数阶的计算操作数量 = " + count);
Console.WriteLine("指数阶(循环实现)的计算操作数量 = " + count);
count = expRecur(n); count = linear(n);
Console.WriteLine("指数阶(递归实现)的计算操作数量 = " + count); Console.WriteLine("线性阶的计算操作数量 = " + count);
count = arrayTraversal(new int[n]);
count = logarithmic((float)n); Console.WriteLine("线性阶(遍历数组)的计算操作数量 = " + count);
Console.WriteLine("对数阶(循环实现)的计算操作数量 = " + count);
count = logRecur((float)n); count = quadratic(n);
Console.WriteLine("对数阶(递归实现)的计算操作数量 = " + count); Console.WriteLine("平方阶的计算操作数量 = " + count);
int[] nums = new int[n];
count = linearLogRecur((float)n); for (int i = 0; i < n; i++)
Console.WriteLine("线性对数阶(递归实现)的计算操作数量 = " + count); nums[i] = n - i; // [n,n-1,...,2,1]
count = bubbleSort(nums);
count = factorialRecur(n); Console.WriteLine("平方阶(冒泡排序)的计算操作数量 = " + count);
Console.WriteLine("阶乘阶(递归实现)的计算操作数量 = " + count);
} count = exponential(n);
Console.WriteLine("指数阶(循环实现)的计算操作数量 = " + count);
count = expRecur(n);
Console.WriteLine("指数阶(递归实现)的计算操作数量 = " + count);
count = logarithmic((float)n);
Console.WriteLine("对数阶(循环实现)的计算操作数量 = " + count);
count = logRecur((float)n);
Console.WriteLine("对数阶(递归实现)的计算操作数量 = " + count);
count = linearLogRecur((float)n);
Console.WriteLine("线性对数阶(递归实现)的计算操作数量 = " + count);
count = factorialRecur(n);
Console.WriteLine("阶乘阶(递归实现)的计算操作数量 = " + count);
} }
} }

View file

@ -6,58 +6,57 @@
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_computational_complexity namespace hello_algo.chapter_computational_complexity;
public class worst_best_time_complexity
{ {
public class worst_best_time_complexity /* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
static int[] randomNumbers(int n)
{ {
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ int[] nums = new int[n];
static int[] randomNumbers(int n) // 生成数组 nums = { 1, 2, 3, ..., n }
for (int i = 0; i < n; i++)
{ {
int[] nums = new int[n]; nums[i] = i + 1;
// 生成数组 nums = { 1, 2, 3, ..., n }
for (int i = 0; i < n; i++)
{
nums[i] = i + 1;
}
// 随机打乱数组元素
for (int i = 0; i < nums.Length; i++)
{
var index = new Random().Next(i, nums.Length);
var tmp = nums[i];
var ran = nums[index];
nums[i] = ran;
nums[index] = tmp;
}
return nums;
} }
/* 查找数组 nums 中数字 1 所在索引 */ // 随机打乱数组元素
static int findOne(int[] nums) for (int i = 0; i < nums.Length; i++)
{ {
for (int i = 0; i < nums.Length; i++) var index = new Random().Next(i, nums.Length);
{ var tmp = nums[i];
// 当元素 1 在数组头部时达到最佳时间复杂度 O(1) var ran = nums[index];
// 当元素 1 在数组尾部时达到最差时间复杂度 O(n) nums[i] = ran;
if (nums[i] == 1) nums[index] = tmp;
return i;
}
return -1;
} }
return nums;
}
/* 查找数组 nums 中数字 1 所在索引 */
/* Driver Code */ static int findOne(int[] nums)
[Test] {
public void Test() for (int i = 0; i < nums.Length; i++)
{ {
for (int i = 0; i < 10; i++) // 当元素 1 在数组头部时达到最佳时间复杂度 O(1)
{ // 当元素 1 在数组尾部时达到最差时间复杂度 O(n)
int n = 100; if (nums[i] == 1)
int[] nums = randomNumbers(n); return i;
int index = findOne(nums); }
Console.WriteLine("\n数组 [ 1, 2, ..., n ] 被打乱后 = " + string.Join(",", nums)); return -1;
Console.WriteLine("数字 1 的索引为 " + index); }
}
/* Driver Code */
[Test]
public void Test()
{
for (int i = 0; i < 10; i++)
{
int n = 100;
int[] nums = randomNumbers(n);
int index = findOne(nums);
Console.WriteLine("\n数组 [ 1, 2, ..., n ] 被打乱后 = " + string.Join(",", nums));
Console.WriteLine("数字 1 的索引为 " + index);
} }
} }
} }

View file

@ -6,159 +6,157 @@
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_hashing namespace hello_algo.chapter_hashing;
{
/* 键值对 int->String */ /* 键值对 int->String */
class Entry class Entry
{
public int key;
public String val;
public Entry(int key, String val)
{ {
public int key; this.key = key;
public String val; this.val = val;
public Entry(int key, String val) }
}
/* 基于数组简易实现的哈希表 */
class ArrayHashMap
{
private List<Entry?> bucket;
public ArrayHashMap()
{
// 初始化一个长度为 100 的桶数组
bucket = new();
for (int i = 0; i < 100; i++)
{ {
this.key = key; bucket.Add(null);
this.val = val;
} }
} }
/* 基于数组简易实现的哈希表 */ /* 哈希函数 */
class ArrayHashMap private int hashFunc(int key)
{ {
private List<Entry?> bucket; int index = key % 100;
public ArrayHashMap() return index;
{ }
// 初始化一个长度为 100 的桶数组
bucket = new ();
for (int i = 0; i < 100; i++)
{
bucket.Add(null);
}
}
/* 哈希函数 */ /* 查询操作 */
private int hashFunc(int key) public String? get(int key)
{ {
int index = key % 100; int index = hashFunc(key);
return index; Entry? pair = bucket[index];
} if (pair == null) return null;
return pair.val;
}
/* 查询操作 */ /* 添加操作 */
public String? get(int key) public void put(int key, String val)
{
Entry pair = new Entry(key, val);
int index = hashFunc(key);
bucket[index] = pair;
}
/* 删除操作 */
public void remove(int key)
{
int index = hashFunc(key);
// 置为 null 代表删除
bucket[index] = null;
}
/* 获取所有键值对 */
public List<Entry> entrySet()
{
List<Entry> entrySet = new();
foreach (Entry? pair in bucket)
{ {
int index = hashFunc(key); if (pair != null)
Entry? pair = bucket[index]; entrySet.Add(pair);
if (pair == null) return null;
return pair.val;
} }
return entrySet;
}
/* 获取所有键 */
public List<int> keySet()
{
List<int> keySet = new();
foreach (Entry? pair in bucket)
{
if (pair != null)
keySet.Add(pair.key);
}
return keySet;
}
/* 获取所有值 */
public List<String> valueSet()
{
List<String> valueSet = new();
foreach (Entry? pair in bucket)
{
if (pair != null)
valueSet.Add(pair.val);
}
return valueSet;
}
/* 打印哈希表 */
public void print()
{
foreach (Entry kv in entrySet())
{
Console.WriteLine(kv.key + " -> " + kv.val);
}
}
}
public class array_hash_map
{
[Test]
public void Test()
{
/* 初始化哈希表 */
ArrayHashMap map = new ArrayHashMap();
/* 添加操作 */ /* 添加操作 */
public void put(int key, String val) // 在哈希表中添加键值对 (key, value)
{ map.put(12836, "小哈");
Entry pair = new Entry(key, val); map.put(15937, "小啰");
int index = hashFunc(key); map.put(16750, "小算");
bucket[index]=pair; map.put(13276, "小法");
} map.put(10583, "小鸭");
Console.WriteLine("\n添加完成后哈希表为\nKey -> Value");
map.print();
/* 查询操作 */
// 向哈希表输入键 key 得到值 value
String? name = map.get(15937);
Console.WriteLine("\n输入学号 15937 ,查询到姓名 " + name);
/* 删除操作 */ /* 删除操作 */
public void remove(int key) // 在哈希表中删除键值对 (key, value)
{ map.remove(10583);
int index = hashFunc(key); Console.WriteLine("\n删除 10583 后,哈希表为\nKey -> Value");
// 置为 null 代表删除 map.print();
bucket[index]=null;
}
/* 获取所有键值对 */ /* 遍历哈希表 */
public List<Entry> entrySet() Console.WriteLine("\n遍历键值对 Key->Value");
foreach (Entry kv in map.entrySet())
{ {
List<Entry> entrySet = new (); Console.WriteLine(kv.key + " -> " + kv.val);
foreach (Entry? pair in bucket)
{
if (pair != null)
entrySet.Add(pair);
}
return entrySet;
} }
Console.WriteLine("\n单独遍历键 Key");
/* 获取所有键 */ foreach (int key in map.keySet())
public List<int> keySet()
{ {
List<int> keySet = new (); Console.WriteLine(key);
foreach (Entry? pair in bucket)
{
if (pair != null)
keySet.Add(pair.key);
}
return keySet;
} }
Console.WriteLine("\n单独遍历值 Value");
/* 获取所有值 */ foreach (String val in map.valueSet())
public List<String> valueSet()
{ {
List<String> valueSet = new (); Console.WriteLine(val);
foreach (Entry? pair in bucket)
{
if (pair != null)
valueSet.Add(pair.val);
}
return valueSet;
}
/* 打印哈希表 */
public void print()
{
foreach (Entry kv in entrySet())
{
Console.WriteLine(kv.key + " -> " + kv.val);
}
} }
} }
}
public class array_hash_map
{
[Test]
public void Test()
{
/* 初始化哈希表 */
ArrayHashMap map = new ArrayHashMap();
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.put(12836, "小哈");
map.put(15937, "小啰");
map.put(16750, "小算");
map.put(13276, "小法");
map.put(10583, "小鸭");
Console.WriteLine("\n添加完成后哈希表为\nKey -> Value");
map.print();
/* 查询操作 */
// 向哈希表输入键 key 得到值 value
String? name = map.get(15937);
Console.WriteLine("\n输入学号 15937 ,查询到姓名 " + name);
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
map.remove(10583);
Console.WriteLine("\n删除 10583 后,哈希表为\nKey -> Value");
map.print();
/* 遍历哈希表 */
Console.WriteLine("\n遍历键值对 Key->Value");
foreach (Entry kv in map.entrySet())
{
Console.WriteLine(kv.key + " -> " + kv.val);
}
Console.WriteLine("\n单独遍历键 Key");
foreach (int key in map.keySet())
{
Console.WriteLine(key);
}
Console.WriteLine("\n单独遍历值 Value");
foreach (String val in map.valueSet())
{
Console.WriteLine(val);
}
}
}
}

View file

@ -8,50 +8,52 @@
using hello_algo.include; using hello_algo.include;
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_hashing namespace hello_algo.chapter_hashing;
public class hash_map
{ {
[Test]
public void Test()
{
/* 初始化哈希表 */
Dictionary<int, String> map = new();
public class hash_map { /* 添加操作 */
[Test] // 在哈希表中添加键值对 (key, value)
public void Test() map.Add(12836, "小哈");
map.Add(15937, "小啰");
map.Add(16750, "小算");
map.Add(13276, "小法");
map.Add(10583, "小鸭");
Console.WriteLine("\n添加完成后哈希表为\nKey -> Value");
PrintUtil.printHashMap(map);
/* 查询操作 */
// 向哈希表输入键 key 得到值 value
String name = map[15937];
Console.WriteLine("\n输入学号 15937 ,查询到姓名 " + name);
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
map.Remove(10583);
Console.WriteLine("\n删除 10583 后,哈希表为\nKey -> Value");
PrintUtil.printHashMap(map);
/* 遍历哈希表 */
Console.WriteLine("\n遍历键值对 Key->Value");
foreach (var kv in map)
{ {
/* 初始化哈希表 */ Console.WriteLine(kv.Key + " -> " + kv.Value);
Dictionary<int, String> map = new (); }
Console.WriteLine("\n单独遍历键 Key");
/* 添加操作 */ foreach (int key in map.Keys)
// 在哈希表中添加键值对 (key, value) {
map.Add(12836, "小哈"); Console.WriteLine(key);
map.Add(15937, "小啰"); }
map.Add(16750, "小算"); Console.WriteLine("\n单独遍历值 Value");
map.Add(13276, "小法"); foreach (String val in map.Values)
map.Add(10583, "小鸭"); {
Console.WriteLine("\n添加完成后哈希表为\nKey -> Value"); Console.WriteLine(val);
PrintUtil.printHashMap(map);
/* 查询操作 */
// 向哈希表输入键 key 得到值 value
String name = map[15937];
Console.WriteLine("\n输入学号 15937 ,查询到姓名 " + name);
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
map.Remove(10583);
Console.WriteLine("\n删除 10583 后,哈希表为\nKey -> Value");
PrintUtil.printHashMap(map);
/* 遍历哈希表 */
Console.WriteLine("\n遍历键值对 Key->Value");
foreach (var kv in map) {
Console.WriteLine(kv.Key + " -> " + kv.Value);
}
Console.WriteLine("\n单独遍历键 Key");
foreach (int key in map.Keys) {
Console.WriteLine(key);
}
Console.WriteLine("\n单独遍历值 Value");
foreach (String val in map.Values) {
Console.WriteLine(val);
}
} }
} }
} }

View file

@ -6,63 +6,62 @@
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_searching namespace hello_algo.chapter_searching;
public class binary_search
{ {
public class binary_search /* 二分查找(双闭区间) */
static int binarySearch(int[] nums, int target)
{ {
/* 二分查找(双闭区间) */ // 初始化双闭区间 [0, n-1] i, j 分别指向数组首元素尾元素
static int binarySearch(int[] nums, int target) int i = 0, j = nums.Length - 1;
// 循环当搜索区间为空时跳出 i > j 时为空
while (i <= j)
{ {
// 初始化双闭区间 [0, n-1] i, j 分别指向数组首元素尾元素 int m = (i + j) / 2; // 计算中点索引 m
int i = 0, j = nums.Length - 1; if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j]
// 循环当搜索区间为空时跳出 i > j 时为空 i = m + 1;
while (i <= j) else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1]
{ j = m - 1;
int m = (i + j) / 2; // 计算中点索引 m else // 找到目标元素返回其索引
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] return m;
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1]
j = m - 1;
else // 找到目标元素返回其索引
return m;
}
// 未找到目标元素返回 -1
return -1;
} }
// 未找到目标元素返回 -1
return -1;
}
/* 二分查找(左闭右开) */
static int binarySearch1(int[] nums, int target)
{
// 初始化左闭右开 [0, n) i, j 分别指向数组首元素尾元素+1
int i = 0, j = nums.Length;
// 循环当搜索区间为空时跳出 i = j 时为空
while (i < j)
{
int m = (i + j) / 2; // 计算中点索引 m
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j)
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m)
j = m;
else // 找到目标元素返回其索引
return m;
}
// 未找到目标元素返回 -1
return -1;
}
[Test]
public void Test()
{
int target = 6;
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 };
/* 二分查找(双闭区间) */
int index = binarySearch(nums, target);
Console.WriteLine("目标元素 6 的索引 = " + index);
/* 二分查找(左闭右开) */ /* 二分查找(左闭右开) */
static int binarySearch1(int[] nums, int target) index = binarySearch1(nums, target);
{ Console.WriteLine("目标元素 6 的索引 = " + index);
// 初始化左闭右开 [0, n) i, j 分别指向数组首元素尾元素+1
int i = 0, j = nums.Length;
// 循环当搜索区间为空时跳出 i = j 时为空
while (i < j)
{
int m = (i + j) / 2; // 计算中点索引 m
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j)
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m)
j = m;
else // 找到目标元素返回其索引
return m;
}
// 未找到目标元素返回 -1
return -1;
}
[Test]
public void Test()
{
int target = 6;
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 };
/* 二分查找(双闭区间) */
int index = binarySearch(nums, target);
Console.WriteLine("目标元素 6 的索引 = " + index);
/* 二分查找(左闭右开) */
index = binarySearch1(nums, target);
Console.WriteLine("目标元素 6 的索引 = " + index);
}
} }
} }

View file

@ -7,54 +7,53 @@
using hello_algo.include; using hello_algo.include;
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_searching namespace hello_algo.chapter_searching;
public class hashing_search
{ {
public class hashing_search /* 哈希查找(数组) */
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)
{
// 哈希表的 key: 目标结点值value: 结点对象
// 若哈希表中无此 key 返回 null
return map.GetValueOrDefault(target);
}
[Test]
public void Test()
{
int target = 3;
/* 哈希查找(数组) */ /* 哈希查找(数组) */
static int hashingSearchArray(Dictionary<int, int> map, int target) int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
// 初始化哈希表
Dictionary<int, int> map = new();
for (int i = 0; i < nums.Length; i++)
{ {
// 哈希表的 key: 目标元素value: 索引 map[nums[i]] = i; // key: 元素value: 索引
// 若哈希表中无此 key 返回 -1
return map.GetValueOrDefault(target, -1);
} }
int index = hashingSearchArray(map, target);
Console.WriteLine("目标元素 3 的索引 = " + index);
/* 哈希查找(链表) */ /* 哈希查找(链表) */
static ListNode? hashingSearchLinkedList(Dictionary<int, ListNode> map, int target) ListNode? head = ListNode.ArrToLinkedList(nums);
// 初始化哈希表
Dictionary<int, ListNode> map1 = new();
while (head != null)
{ {
map1[head.val] = head; // key: 结点值value: 结点
// 哈希表的 key: 目标结点值value: 结点对象 head = head.next;
// 若哈希表中无此 key 返回 null
return map.GetValueOrDefault(target);
}
[Test]
public void Test()
{
int target = 3;
/* 哈希查找(数组) */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
// 初始化哈希表
Dictionary<int, int> map = new();
for (int i = 0; i < nums.Length; i++)
{
map[nums[i]] = i; // key: 元素value: 索引
}
int index = hashingSearchArray(map, target);
Console.WriteLine("目标元素 3 的索引 = " + index);
/* 哈希查找(链表) */
ListNode? head = ListNode.ArrToLinkedList(nums);
// 初始化哈希表
Dictionary<int, ListNode> map1 = new();
while (head != null)
{
map1[head.val] = head; // key: 结点值value: 结点
head = head.next;
}
ListNode? node = hashingSearchLinkedList(map1, target);
Console.WriteLine("目标结点值 3 的对应结点对象为 " + node);
} }
ListNode? node = hashingSearchLinkedList(map1, target);
Console.WriteLine("目标结点值 3 的对应结点对象为 " + node);
} }
} }

View file

@ -7,53 +7,52 @@
using hello_algo.include; using hello_algo.include;
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_searching namespace hello_algo.chapter_searching;
public class linear_search
{ {
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++)
{ {
// 遍历数组 // 找到目标元素返回其索引
for (int i = 0; i < nums.Length; i++) if (nums[i] == target)
{ return i;
// 找到目标元素返回其索引
if (nums[i] == target)
return i;
}
// 未找到目标元素返回 -1
return -1;
} }
// 未找到目标元素返回 -1
return -1;
}
/* 线性查找(链表) */ /* 线性查找(链表) */
static ListNode? linearSearchLinkedList(ListNode head, int target) static ListNode? linearSearchLinkedList(ListNode head, int target)
{
// 遍历链表
while (head != null)
{ {
// 遍历链表 // 找到目标结点返回之
while (head != null) if (head.val == target)
{ return head;
// 找到目标结点返回之 head = head.next;
if (head.val == target)
return head;
head = head.next;
}
// 未找到目标结点返回 null
return null;
} }
// 未找到目标结点返回 null
return null;
}
[Test] [Test]
public void Test() public void Test()
{ {
int target = 3; int target = 3;
/* 在数组中执行线性查找 */ /* 在数组中执行线性查找 */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 }; int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
int index = linearSearchArray(nums, target); int index = linearSearchArray(nums, target);
Console.WriteLine("目标元素 3 的索引 = " + index); Console.WriteLine("目标元素 3 的索引 = " + index);
/* 在链表中执行线性查找 */ /* 在链表中执行线性查找 */
ListNode head = ListNode.ArrToLinkedList(nums); ListNode head = ListNode.ArrToLinkedList(nums);
ListNode? node = linearSearchLinkedList(head, target); ListNode? node = linearSearchLinkedList(head, target);
Console.WriteLine("目标结点值 3 的对应结点对象为 " + node); Console.WriteLine("目标结点值 3 的对应结点对象为 " + node);
}
} }
} }

View file

@ -6,63 +6,62 @@
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_sorting namespace hello_algo.chapter_sorting;
public class bubble_sort
{ {
public class bubble_sort /* 冒泡排序 */
static void bubbleSort(int[] nums)
{ {
/* 冒泡排序 */ // 外循环待排序元素数量为 n-1, n-2, ..., 1
static void bubbleSort(int[] nums) for (int i = nums.Length - 1; i > 0; i--)
{ {
// 外循环待排序元素数量为 n-1, n-2, ..., 1 // 内循环冒泡操作
for (int i = nums.Length - 1; i > 0; i--) for (int j = 0; j < i; j++)
{ {
// 内循环冒泡操作 if (nums[j] > nums[j + 1])
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] = nums[j + 1];
int tmp = nums[j]; nums[j + 1] = tmp;
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
}
} }
} }
} }
}
/* 冒泡排序(标志优化)*/
static void bubbleSortWithFlag(int[] nums) /* 冒泡排序(标志优化)*/
{ static void bubbleSortWithFlag(int[] nums)
// 外循环待排序元素数量为 n-1, n-2, ..., 1 {
for (int i = nums.Length - 1; i > 0; i--) // 外循环待排序元素数量为 n-1, n-2, ..., 1
{ for (int i = nums.Length - 1; i > 0; i--)
bool flag = false; // 初始化标志位 {
// 内循环冒泡操作 bool flag = false; // 初始化标志位
for (int j = 0; j < i; j++) // 内循环冒泡操作
{ for (int j = 0; j < i; j++)
if (nums[j] > nums[j + 1]) {
{ if (nums[j] > nums[j + 1])
// 交换 nums[j] nums[j + 1] {
int tmp = nums[j]; // 交换 nums[j] nums[j + 1]
nums[j] = nums[j + 1]; int tmp = nums[j];
nums[j + 1] = tmp; nums[j] = nums[j + 1];
flag = true; // 记录交换元素 nums[j + 1] = tmp;
} flag = true; // 记录交换元素
} }
if (!flag) break; // 此轮冒泡未交换任何元素直接跳出 }
} if (!flag) break; // 此轮冒泡未交换任何元素直接跳出
} }
}
[Test]
public void Test() [Test]
{ public void Test()
int[] nums = { 4, 1, 3, 1, 5, 2 }; {
bubbleSort(nums); int[] nums = { 4, 1, 3, 1, 5, 2 };
Console.WriteLine("冒泡排序完成后 nums = " + string.Join(",",nums)); bubbleSort(nums);
Console.WriteLine("冒泡排序完成后 nums = " + string.Join(",", nums));
int[] nums1 = { 4, 1, 3, 1, 5, 2 };
bubbleSortWithFlag(nums1); int[] nums1 = { 4, 1, 3, 1, 5, 2 };
Console.WriteLine("冒泡排序完成后 nums1 = " + string.Join(",", nums)); bubbleSortWithFlag(nums1);
} Console.WriteLine("冒泡排序完成后 nums1 = " + string.Join(",", nums));
} }
} }

View file

@ -6,33 +6,32 @@
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_sorting namespace hello_algo.chapter_sorting;
{
public class insertion_sort
{
/* 插入排序 */
static void insertionSort(int[] nums)
{
// 外循环base = nums[1], nums[2], ..., nums[n-1]
for (int i = 1; i < nums.Length; i++)
{
int bas = nums[i], j = i - 1;
// 内循环 base 插入到左边的正确位置
while (j >= 0 && nums[j] > bas)
{
nums[j + 1] = nums[j]; // 1. nums[j] 向右移动一位
j--;
}
nums[j + 1] = bas; // 2. base 赋值到正确位置
}
}
[Test] public class insertion_sort
public void Test() {
/* 插入排序 */
static void insertionSort(int[] nums)
{
// 外循环base = nums[1], nums[2], ..., nums[n-1]
for (int i = 1; i < nums.Length; i++)
{ {
int[] nums = { 4, 1, 3, 1, 5, 2 }; int bas = nums[i], j = i - 1;
insertionSort(nums); // 内循环 base 插入到左边的正确位置
Console.WriteLine("插入排序完成后 nums = " + string.Join(",", nums)); while (j >= 0 && nums[j] > bas)
{
nums[j + 1] = nums[j]; // 1. nums[j] 向右移动一位
j--;
}
nums[j + 1] = bas; // 2. base 赋值到正确位置
} }
} }
[Test]
public void Test()
{
int[] nums = { 4, 1, 3, 1, 5, 2 };
insertionSort(nums);
Console.WriteLine("插入排序完成后 nums = " + string.Join(",", nums));
}
} }

View file

@ -6,60 +6,57 @@
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_sorting namespace hello_algo.chapter_sorting;
public class merge_sort
{ {
public class merge_sort /* 合并左子数组和右子数组 */
// 左子数组区间 [left, mid]
// 右子数组区间 [mid + 1, right]
static void merge(int[] nums, int left, int mid, int right)
{ {
/** // 初始化辅助数组
* 合并左子数组和右子数组 int[] tmp = nums[left..(right + 1)];
* 左子数组区间 [left, mid] // 左子数组的起始索引和结束索引
* 右子数组区间 [mid + 1, right] int leftStart = left - left, leftEnd = mid - left;
*/ // 右子数组的起始索引和结束索引
static void merge(int[] nums, int left, int mid, int right) int rightStart = mid + 1 - left, rightEnd = right - left;
// i, j 分别指向左子数组右子数组的首元素
int i = leftStart, j = rightStart;
// 通过覆盖原数组 nums 来合并左子数组和右子数组
for (int k = left; k <= right; k++)
{ {
// 初始化辅助数组 // 左子数组已全部合并完则选取右子数组元素并且 j++
int[] tmp = nums[left..(right + 1)]; if (i > leftEnd)
// 左子数组的起始索引和结束索引 nums[k] = tmp[j++];
int leftStart = left - left, leftEnd = mid - left; // 否则右子数组已全部合并完左子数组元素 <= 右子数组元素则选取左子数组元素并且 i++
// 右子数组的起始索引和结束索引 else if (j > rightEnd || tmp[i] <= tmp[j])
int rightStart = mid + 1 - left, rightEnd = right - left; nums[k] = tmp[i++];
// i, j 分别指向左子数组右子数组的首元素 // 否则左右子数组都未全部合并完左子数组元素 > 右子数组元素则选取右子数组元素并且 j++
int i = leftStart, j = rightStart; else
// 通过覆盖原数组 nums 来合并左子数组和右子数组 nums[k] = tmp[j++];
for (int k = left; k <= right; k++)
{
// 左子数组已全部合并完则选取右子数组元素并且 j++
if (i > leftEnd)
nums[k] = tmp[j++];
// 否则右子数组已全部合并完左子数组元素 <= 右子数组元素则选取左子数组元素并且 i++
else if (j > rightEnd || tmp[i] <= tmp[j])
nums[k] = tmp[i++];
// 否则左右子数组都未全部合并完左子数组元素 > 右子数组元素则选取右子数组元素并且 j++
else
nums[k] = tmp[j++];
}
}
/* 归并排序 */
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); // 递归右子数组
// 合并阶段
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);
Console.WriteLine("归并排序完成后 nums = " + string.Join(",", nums));
} }
} }
/* 归并排序 */
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); // 递归右子数组
// 合并阶段
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);
Console.WriteLine("归并排序完成后 nums = " + string.Join(",", nums));
}
} }

View file

@ -6,178 +6,177 @@
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_sorting namespace hello_algo.chapter_sorting;
class QuickSort
{ {
class QuickSort /* 元素交换 */
static void swap(int[] nums, int i, int j)
{ {
/* 元素交换 */ int tmp = nums[i];
static void swap(int[] nums, int i, int j) nums[i] = nums[j];
{ nums[j] = tmp;
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* 哨兵划分 */
static int partition(int[] nums, int left, int right)
{
// nums[left] 作为基准数
int i = left, j = right;
while (i < j)
{
while (i < j && nums[j] >= nums[left])
j--; // 从右向左找首个小于基准数的元素
while (i < j && nums[i] <= nums[left])
i++; // 从左向右找首个大于基准数的元素
swap(nums, i, j); // 交换这两个元素
}
swap(nums, i, left); // 将基准数交换至两子数组的分界线
return i; // 返回基准数的索引
}
/* 快速排序 */
public static void quickSort(int[] nums, int left, int right)
{
// 子数组长度为 1 时终止递归
if (left >= right)
return;
// 哨兵划分
int pivot = partition(nums, left, right);
// 递归左子数组右子数组
quickSort(nums, left, pivot - 1);
quickSort(nums, pivot + 1, right);
}
} }
/* 快速排序类(中位基准数优化) */ /* 哨兵划分 */
class QuickSortMedian static int partition(int[] nums, int left, int right)
{ {
/* 元素交换 */ // nums[left] 作为基准数
static void swap(int[] nums, int i, int j) int i = left, j = right;
while (i < j)
{ {
int tmp = nums[i]; while (i < j && nums[j] >= nums[left])
nums[i] = nums[j]; j--; // 从右向左找首个小于基准数的元素
nums[j] = tmp; while (i < j && nums[i] <= nums[left])
i++; // 从左向右找首个大于基准数的元素
swap(nums, i, j); // 交换这两个元素
} }
swap(nums, i, left); // 将基准数交换至两子数组的分界线
return i; // 返回基准数的索引
}
/* 选取三个元素的中位数 */ /* 快速排序 */
static int medianThree(int[] nums, int left, int mid, int right) public static void quickSort(int[] nums, int left, int right)
{
// 子数组长度为 1 时终止递归
if (left >= right)
return;
// 哨兵划分
int pivot = partition(nums, left, 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 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]))
return left;
else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))
return mid;
else
return right;
}
/* 哨兵划分(三数取中值) */
static int partition(int[] nums, int left, int right)
{
// 选取三个候选元素的中位数
int med = medianThree(nums, left, (left + right) / 2, right);
// 将中位数交换至数组最左端
swap(nums, left, med);
// nums[left] 作为基准数
int i = left, j = right;
while (i < j)
{ {
// 使用了异或操作来简化代码 while (i < j && nums[j] >= nums[left])
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1 j--; // 从右向左找首个小于基准数的元素
if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) while (i < j && nums[i] <= nums[left])
return left; i++; // 从左向右找首个大于基准数的元素
else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right])) swap(nums, i, j); // 交换这两个元素
return mid; }
swap(nums, i, left); // 将基准数交换至两子数组的分界线
return i; // 返回基准数的索引
}
/* 快速排序 */
public static void quickSort(int[] nums, int left, int right)
{
// 子数组长度为 1 时终止递归
if (left >= right)
return;
// 哨兵划分
int pivot = partition(nums, left, 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 int partition(int[] nums, int left, int right)
{
// nums[left] 作为基准数
int i = left, j = right;
while (i < j)
{
while (i < j && nums[j] >= nums[left])
j--; // 从右向左找首个小于基准数的元素
while (i < j && nums[i] <= nums[left])
i++; // 从左向右找首个大于基准数的元素
swap(nums, i, j); // 交换这两个元素
}
swap(nums, i, left); // 将基准数交换至两子数组的分界线
return i; // 返回基准数的索引
}
/* 快速排序(尾递归优化) */
public static void quickSort(int[] nums, int left, int right)
{
// 子数组长度为 1 时终止
while (left < right)
{
// 哨兵划分操作
int pivot = partition(nums, left, right);
// 对两个子数组中较短的那个执行快排
if (pivot - left < right - pivot)
{
quickSort(nums, left, pivot - 1); // 递归排序左子数组
left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right]
}
else else
return right;
}
/* 哨兵划分(三数取中值) */
static int partition(int[] nums, int left, int right)
{
// 选取三个候选元素的中位数
int med = medianThree(nums, left, (left + right) / 2, right);
// 将中位数交换至数组最左端
swap(nums, left, med);
// nums[left] 作为基准数
int i = left, j = right;
while (i < j)
{ {
while (i < j && nums[j] >= nums[left]) quickSort(nums, pivot + 1, right); // 递归排序右子数组
j--; // 从右向左找首个小于基准数的元素 right = pivot - 1; // 剩余待排序区间为 [left, pivot - 1]
while (i < j && nums[i] <= nums[left])
i++; // 从左向右找首个大于基准数的元素
swap(nums, i, j); // 交换这两个元素
} }
swap(nums, i, left); // 将基准数交换至两子数组的分界线
return i; // 返回基准数的索引
}
/* 快速排序 */
public static void quickSort(int[] nums, int left, int right)
{
// 子数组长度为 1 时终止递归
if (left >= right)
return;
// 哨兵划分
int pivot = partition(nums, left, 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 int partition(int[] nums, int left, int right)
{
// nums[left] 作为基准数
int i = left, j = right;
while (i < j)
{
while (i < j && nums[j] >= nums[left])
j--; // 从右向左找首个小于基准数的元素
while (i < j && nums[i] <= nums[left])
i++; // 从左向右找首个大于基准数的元素
swap(nums, i, j); // 交换这两个元素
}
swap(nums, i, left); // 将基准数交换至两子数组的分界线
return i; // 返回基准数的索引
}
/* 快速排序(尾递归优化) */
public static void quickSort(int[] nums, int left, int right)
{
// 子数组长度为 1 时终止
while (left < right)
{
// 哨兵划分操作
int pivot = partition(nums, left, right);
// 对两个子数组中较短的那个执行快排
if (pivot - left < right - pivot)
{
quickSort(nums, left, pivot - 1); // 递归排序左子数组
left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right]
}
else
{
quickSort(nums, pivot + 1, right); // 递归排序右子数组
right = pivot - 1; // 剩余待排序区间为 [left, pivot - 1]
}
}
}
}
public class quick_sort
{
[Test]
public void Test()
{
/* 快速排序 */
int[] nums = { 2, 4, 1, 0, 3, 5 };
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);
Console.WriteLine("快速排序(中位基准数优化)完成后 nums1 = " + string.Join(",", nums1));
/* 快速排序(尾递归优化) */
int[] nums2 = { 2, 4, 1, 0, 3, 5 };
QuickSortTailCall.quickSort(nums2, 0, nums2.Length - 1);
Console.WriteLine("快速排序(尾递归优化)完成后 nums2 = " + string.Join(",", nums2));
} }
} }
} }
public class quick_sort
{
[Test]
public void Test()
{
/* 快速排序 */
int[] nums = { 2, 4, 1, 0, 3, 5 };
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);
Console.WriteLine("快速排序(中位基准数优化)完成后 nums1 = " + string.Join(",", nums1));
/* 快速排序(尾递归优化) */
int[] nums2 = { 2, 4, 1, 0, 3, 5 };
QuickSortTailCall.quickSort(nums2, 0, nums2.Length - 1);
Console.WriteLine("快速排序(尾递归优化)完成后 nums2 = " + string.Join(",", nums2));
}
}

View file

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

View file

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

View file

@ -6,44 +6,43 @@
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_stack_and_queue namespace hello_algo.chapter_stack_and_queue;
public class deque
{ {
public class deque [Test]
public void Test()
{ {
[Test] /* 初始化双向队列 */
public void Test() // C# 将链表 LinkedList 看作双向队列来使用
{ LinkedList<int> deque = new LinkedList<int>();
/* 初始化双向队列 */
// C# 将链表 LinkedList 看作双向队列来使用
LinkedList<int> deque = new LinkedList<int>();
/* 元素入队 */ /* 元素入队 */
deque.AddLast(2); // 添加至队尾 deque.AddLast(2); // 添加至队尾
deque.AddLast(5); deque.AddLast(5);
deque.AddLast(4); deque.AddLast(4);
deque.AddFirst(3); // 添加至队首 deque.AddFirst(3); // 添加至队首
deque.AddFirst(1); deque.AddFirst(1);
Console.WriteLine("双向队列 deque = " + String.Join(",", deque.ToArray())); Console.WriteLine("双向队列 deque = " + String.Join(",", deque.ToArray()));
/* 访问元素 */ /* 访问元素 */
int peekFirst = deque.First.Value; // 队首元素 int peekFirst = deque.First.Value; // 队首元素
Console.WriteLine("队首元素 peekFirst = " + peekFirst); Console.WriteLine("队首元素 peekFirst = " + peekFirst);
int peekLast = deque.Last.Value; // 队尾元素 int peekLast = deque.Last.Value; // 队尾元素
Console.WriteLine("队尾元素 peekLast = " + peekLast); Console.WriteLine("队尾元素 peekLast = " + peekLast);
/* 元素出队 */ /* 元素出队 */
deque.RemoveFirst(); // 队首元素出队 deque.RemoveFirst(); // 队首元素出队
Console.WriteLine("队首元素出队后 deque = " + String.Join(",", deque.ToArray())); Console.WriteLine("队首元素出队后 deque = " + String.Join(",", deque.ToArray()));
deque.RemoveLast(); // 队尾元素出队 deque.RemoveLast(); // 队尾元素出队
Console.WriteLine("队尾元素出队后 deque = " + String.Join(",", deque.ToArray())); Console.WriteLine("队尾元素出队后 deque = " + String.Join(",", deque.ToArray()));
/* 获取双向队列的长度 */ /* 获取双向队列的长度 */
int size = deque.Count; int size = deque.Count;
Console.WriteLine("双向队列长度 size = " + size); Console.WriteLine("双向队列长度 size = " + size);
/* 判断双向队列是否为空 */ /* 判断双向队列是否为空 */
bool isEmpty = deque.Count == 0; bool isEmpty = deque.Count == 0;
Console.WriteLine("双向队列是否为空 = " + isEmpty); Console.WriteLine("双向队列是否为空 = " + isEmpty);
}
} }
} }

View file

@ -7,118 +7,117 @@
using hello_algo.include; using hello_algo.include;
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_stack_and_queue namespace hello_algo.chapter_stack_and_queue;
/* 基于链表实现的队列 */
class LinkedListQueue
{ {
/* 基于链表实现的队列 */ private ListNode? front, rear; // 头结点 front 尾结点 rear
class LinkedListQueue private int queSize = 0;
public LinkedListQueue()
{ {
private ListNode? front, rear; // 头结点 front 尾结点 rear front = null;
private int queSize = 0; rear = null;
}
public LinkedListQueue() /* 获取队列的长度 */
{ public int size()
front = null; {
rear = null; return queSize;
} }
/* 获取队列的长度 */ /* 判断队列是否为空 */
public int size() public bool isEmpty()
{ {
return queSize; return size() == 0;
} }
/* 判断队列是否为空 */ /* 入队 */
public bool isEmpty() public void push(int num)
{
// 尾结点后添加 num
ListNode node = new ListNode(num);
// 如果队列为空则令头尾结点都指向该结点
if (front == null)
{ {
return size() == 0; front = node;
rear = node;
// 如果队列不为空则将该结点添加到尾结点后
} }
else if (rear != null)
{
rear.next = node;
rear = node;
}
queSize++;
}
/* 入队 */ /* 出队 */
public void push(int num) public int poll()
{ {
// 尾结点后添加 num int num = peek();
ListNode node = new ListNode(num); // 删除头结点
// 如果队列为空则令头尾结点都指向该结点 front = front?.next;
if (front == null) queSize--;
{ return num;
front = node; }
rear = node;
// 如果队列不为空则将该结点添加到尾结点后
}
else if (rear != null)
{
rear.next = node;
rear = node;
}
queSize++;
}
/* 出队 */ /* 访问队首元素 */
public int poll() public int peek()
{
if (size() == 0 || front == null)
throw new Exception();
return front.val;
}
/* 将链表转化为 Array 并返回 */
public int[] toArray()
{
if (front == null)
return Array.Empty<int>();
ListNode node = front;
int[] res = new int[size()];
for (int i = 0; i < res.Length; i++)
{ {
int num = peek(); res[i] = node.val;
// 删除头结点 node = node.next;
front = front?.next;
queSize--;
return num;
} }
return res;
}
}
public class linkedlist_queue
{
[Test]
public void Test()
{
/* 初始化队列 */
LinkedListQueue queue = new LinkedListQueue();
/* 元素入队 */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
Console.WriteLine("队列 queue = " + String.Join(",", queue.toArray()));
/* 访问队首元素 */ /* 访问队首元素 */
public int peek() int peek = queue.peek();
{ Console.WriteLine("队首元素 peek = " + peek);
if (size() == 0 || front == null)
throw new Exception();
return front.val;
}
/* 将链表转化为 Array 并返回 */ /* 元素出队 */
public int[] toArray() int poll = queue.poll();
{ Console.WriteLine("出队元素 poll = " + poll + ",出队后 queue = " + String.Join(",", queue.toArray()));
if (front == null)
return Array.Empty<int>();
ListNode node = front; /* 获取队列的长度 */
int[] res = new int[size()]; int size = queue.size();
for (int i = 0; i < res.Length; i++) Console.WriteLine("队列长度 size = " + size);
{
res[i] = node.val; /* 判断队列是否为空 */
node = node.next; bool isEmpty = queue.isEmpty();
} Console.WriteLine("队列是否为空 = " + isEmpty);
return res;
}
} }
}
public class linkedlist_queue
{
[Test]
public void Test()
{
/* 初始化队列 */
LinkedListQueue queue = new LinkedListQueue();
/* 元素入队 */
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();
Console.WriteLine("队首元素 peek = " + peek);
/* 元素出队 */
int poll = queue.poll();
Console.WriteLine("出队元素 poll = " + poll + ",出队后 queue = " + String.Join(",", queue.toArray()));
/* 获取队列的长度 */
int size = queue.size();
Console.WriteLine("队列长度 size = " + size);
/* 判断队列是否为空 */
bool isEmpty = queue.isEmpty();
Console.WriteLine("队列是否为空 = " + isEmpty);
}
}
}

View file

@ -7,107 +7,107 @@
using hello_algo.include; using hello_algo.include;
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_stack_and_queue namespace hello_algo.chapter_stack_and_queue;
/* 基于链表实现的栈 */
class LinkedListStack
{ {
class LinkedListStack private ListNode? stackPeek; // 将头结点作为栈顶
private int stkSize = 0; // 栈的长度
public LinkedListStack()
{ {
private ListNode? stackPeek; // 将头结点作为栈顶 stackPeek = null;
private int stkSize = 0; // 栈的长度 }
public LinkedListStack() /* 获取栈的长度 */
public int size()
{
return stkSize;
}
/* 判断栈是否为空 */
public bool isEmpty()
{
return size() == 0;
}
/* 入栈 */
public void push(int num)
{
ListNode node = new ListNode(num);
node.next = stackPeek;
stackPeek = node;
stkSize++;
}
/* 出栈 */
public int pop()
{
if (stackPeek == null)
throw new Exception();
int num = peek();
stackPeek = stackPeek.next;
stkSize--;
return num;
}
/* 访问栈顶元素 */
public int peek()
{
if (size() == 0 || stackPeek == null)
throw new Exception();
return stackPeek.val;
}
/* 将 List 转化为 Array 并返回 */
public int[] toArray()
{
if (stackPeek == null)
return Array.Empty<int>();
ListNode node = stackPeek;
int[] res = new int[size()];
for (int i = res.Length - 1; i >= 0; i--)
{ {
stackPeek = null; res[i] = node.val;
node = node.next;
} }
return res;
}
}
/* 获取栈的长度 */ public class linkedlist_stack
public int size() {
{ [Test]
return stkSize; public void Test()
} {
/* 初始化栈 */
LinkedListStack stack = new LinkedListStack();
/* 判断栈是否为空 */ /* 元素入栈 */
public bool isEmpty() stack.push(1);
{ stack.push(3);
return size() == 0; stack.push(2);
} stack.push(5);
stack.push(4);
/* 入栈 */ Console.WriteLine("栈 stack = " + String.Join(",", stack.toArray()));
public void push(int num)
{
ListNode node = new ListNode(num);
node.next = stackPeek;
stackPeek = node;
stkSize++;
}
/* 出栈 */
public int pop()
{
if (stackPeek == null)
throw new Exception();
int num = peek();
stackPeek = stackPeek.next;
stkSize--;
return num;
}
/* 访问栈顶元素 */ /* 访问栈顶元素 */
public int peek() int peek = stack.peek();
{ Console.WriteLine("栈顶元素 peek = " + peek);
if (size() == 0 || stackPeek==null)
throw new Exception();
return stackPeek.val;
}
/* 将 List 转化为 Array 并返回 */ /* 元素出栈 */
public int[] toArray() int pop = stack.pop();
{ Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + String.Join(",", stack.toArray()));
if (stackPeek == null)
return Array.Empty<int>();
ListNode node = stackPeek; /* 获取栈的长度 */
int[] res = new int[size()]; int size = stack.size();
for (int i = res.Length - 1; i >= 0; i--) Console.WriteLine("栈的长度 size = " + size);
{
res[i] = node.val; /* 判断是否为空 */
node = node.next; bool isEmpty = stack.isEmpty();
} Console.WriteLine("栈是否为空 = " + isEmpty);
return res;
}
} }
}
public class linkedlist_stack
{
[Test]
public void Test()
{
/* 初始化栈 */
LinkedListStack stack = new LinkedListStack();
/* 元素入栈 */
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();
Console.WriteLine("栈顶元素 peek = " + peek);
/* 元素出栈 */
int pop = stack.pop();
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + String.Join(",",stack.toArray()));
/* 获取栈的长度 */
int size = stack.size();
Console.WriteLine("栈的长度 size = " + size);
/* 判断是否为空 */
bool isEmpty = stack.isEmpty();
Console.WriteLine("栈是否为空 = " + isEmpty);
}
}
}

View file

@ -6,40 +6,38 @@
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_stack_and_queue namespace hello_algo.chapter_stack_and_queue;
public class queue
{ {
public class queue [Test]
public void Test()
{ {
[Test] /* 初始化队列 */
public void Test() Queue<int> queue = new();
{
/* 初始化队列 */
Queue<int> queue = new();
/* 元素入队 */ /* 元素入队 */
queue.Enqueue(1); queue.Enqueue(1);
queue.Enqueue(3); queue.Enqueue(3);
queue.Enqueue(2); queue.Enqueue(2);
queue.Enqueue(5); queue.Enqueue(5);
queue.Enqueue(4); queue.Enqueue(4);
Console.WriteLine("队列 queue = " + String.Join(",", queue.ToArray())); Console.WriteLine("队列 queue = " + String.Join(",", queue.ToArray()));
/* 访问队首元素 */ /* 访问队首元素 */
int peek = queue.Peek(); int peek = queue.Peek();
Console.WriteLine("队首元素 peek = " + peek); Console.WriteLine("队首元素 peek = " + peek);
/* 元素出队 */ /* 元素出队 */
int poll = queue.Dequeue(); int poll = queue.Dequeue();
Console.WriteLine("出队元素 poll = " + poll + ",出队后 queue = " + String.Join(",", queue.ToArray())); Console.WriteLine("出队元素 poll = " + poll + ",出队后 queue = " + String.Join(",", queue.ToArray()));
/* 获取队列的长度 */ /* 获取队列的长度 */
int size = queue.Count(); int size = queue.Count();
Console.WriteLine("队列长度 size = " + size); Console.WriteLine("队列长度 size = " + size);
/* 判断队列是否为空 */ /* 判断队列是否为空 */
bool isEmpty = queue.Count() == 0; bool isEmpty = queue.Count() == 0;
Console.WriteLine("队列是否为空 = " + isEmpty); Console.WriteLine("队列是否为空 = " + isEmpty);
}
} }
} }

View file

@ -6,40 +6,39 @@
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_stack_and_queue namespace hello_algo.chapter_stack_and_queue;
public class stack
{ {
public class stack [Test]
public void Test()
{ {
[Test] /* 初始化栈 */
public void Test() Stack<int> stack = new();
{
/* 初始化栈 */
Stack<int> stack = new();
/* 元素入栈 */ /* 元素入栈 */
stack.Push(1); stack.Push(1);
stack.Push(3); stack.Push(3);
stack.Push(2); stack.Push(2);
stack.Push(5); stack.Push(5);
stack.Push(4); stack.Push(4);
// 请注意stack.ToArray() 得到的是倒序序列即索引 0 为栈顶 // 请注意stack.ToArray() 得到的是倒序序列即索引 0 为栈顶
Console.WriteLine("栈 stack = " + string.Join(",", stack.ToArray())); Console.WriteLine("栈 stack = " + string.Join(",", stack.ToArray()));
/* 访问栈顶元素 */ /* 访问栈顶元素 */
int peek = stack.Peek(); int peek = stack.Peek();
Console.WriteLine("栈顶元素 peek = " + peek); Console.WriteLine("栈顶元素 peek = " + peek);
/* 元素出栈 */ /* 元素出栈 */
int pop = stack.Pop(); int pop = stack.Pop();
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack.ToArray())); Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack.ToArray()));
/* 获取栈的长度 */ /* 获取栈的长度 */
int size = stack.Count(); int size = stack.Count();
Console.WriteLine("栈的长度 size = " + size); Console.WriteLine("栈的长度 size = " + size);
/* 判断是否为空 */ /* 判断是否为空 */
bool isEmpty = stack.Count() == 0; bool isEmpty = stack.Count() == 0;
Console.WriteLine("栈是否为空 = " + isEmpty); Console.WriteLine("栈是否为空 = " + isEmpty);
}
} }
} }

View file

@ -7,254 +7,253 @@
using hello_algo.include; using hello_algo.include;
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_tree namespace hello_algo.chapter_tree;
/* AVL 树 */
class AVLTree
{ {
/* AVL 树 */ public TreeNode? root; // 根结点
class AVLTree
/* 获取结点高度 */
public int height(TreeNode? node)
{ {
public TreeNode? root; // 根结点 // 空结点高度为 -1 叶结点高度为 0
return node == null ? -1 : node.height;
/* 获取结点高度 */
public int height(TreeNode? node)
{
// 空结点高度为 -1 叶结点高度为 0
return node == null ? -1 : node.height;
}
/* 更新结点高度 */
private void updateHeight(TreeNode node)
{
// 结点高度等于最高子树高度 + 1
node.height = Math.Max(height(node.left), height(node.right)) + 1;
}
/* 获取平衡因子 */
public int balanceFactor(TreeNode? node)
{
// 空结点平衡因子为 0
if (node == null) return 0;
// 结点平衡因子 = 左子树高度 - 右子树高度
return height(node.left) - height(node.right);
}
/* 右旋操作 */
TreeNode? rightRotate(TreeNode? node)
{
TreeNode? child = node.left;
TreeNode? grandChild = child?.right;
// child 为原点 node 向右旋转
child.right = node;
node.left = grandChild;
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根结点
return child;
}
/* 左旋操作 */
TreeNode? leftRotate(TreeNode? node)
{
TreeNode? child = node.right;
TreeNode? grandChild = child?.left;
// child 为原点 node 向左旋转
child.left = node;
node.right = grandChild;
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根结点
return child;
}
/* 执行旋转操作,使该子树重新恢复平衡 */
TreeNode? rotate(TreeNode? node)
{
// 获取结点 node 的平衡因子
int balanceFactorInt = balanceFactor(node);
// 左偏树
if (balanceFactorInt > 1)
{
if (balanceFactor(node.left) >= 0)
{
// 右旋
return rightRotate(node);
}
else
{
// 先左旋后右旋
node.left = leftRotate(node?.left);
return rightRotate(node);
}
}
// 右偏树
if (balanceFactorInt < -1)
{
if (balanceFactor(node.right) <= 0)
{
// 左旋
return leftRotate(node);
}
else
{
// 先右旋后左旋
node.right = rightRotate(node?.right);
return leftRotate(node);
}
}
// 平衡树无需旋转直接返回
return node;
}
/* 插入结点 */
public TreeNode? insert(int val)
{
root = insertHelper(root, val);
return root;
}
/* 递归插入结点(辅助函数) */
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);
else if (val > node.val)
node.right = insertHelper(node.right, val);
else
return node; // 重复结点不插入直接返回
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根结点
return node;
}
/* 删除结点 */
public TreeNode? remove(int val)
{
root = removeHelper(root, val);
return root;
}
/* 递归删除结点(辅助函数) */
private TreeNode? removeHelper(TreeNode? node, int val)
{
if (node == null) return null;
/* 1. 查找结点,并删除之 */
if (val < node.val)
node.left = removeHelper(node.left, val);
else if (val > node.val)
node.right = removeHelper(node.right, val);
else
{
if (node.left == null || node.right == null)
{
TreeNode? child = node.left != null ? node.left : node.right;
// 子结点数量 = 0 直接删除 node 并返回
if (child == null)
return null;
// 子结点数量 = 1 直接删除 node
else
node = child;
}
else
{
// 子结点数量 = 2 则将中序遍历的下个结点删除并用该结点替换当前结点
TreeNode? temp = getInOrderNext(node.right);
node.right = removeHelper(node.right, temp.val);
node.val = temp.val;
}
}
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根结点
return node;
}
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
private TreeNode? getInOrderNext(TreeNode? node)
{
if (node == null) return node;
// 循环访问左子结点直到叶结点时为最小结点跳出
while (node.left != null)
{
node = node.left;
}
return node;
}
/* 查找结点 */
public TreeNode? search(int val)
{
TreeNode? cur = root;
// 循环查找越过叶结点后跳出
while (cur != null)
{
// 目标结点在 cur 的右子树中
if (cur.val < val)
cur = cur.right;
// 目标结点在 cur 的左子树中
else if (cur.val > val)
cur = cur.left;
// 找到目标结点跳出循环
else
break;
}
// 返回目标结点
return cur;
}
} }
public class avl_tree /* 更新结点高度 */
private void updateHeight(TreeNode node)
{ {
static void testInsert(AVLTree tree, int val) // 结点高度等于最高子树高度 + 1
node.height = Math.Max(height(node.left), height(node.right)) + 1;
}
/* 获取平衡因子 */
public int balanceFactor(TreeNode? node)
{
// 空结点平衡因子为 0
if (node == null) return 0;
// 结点平衡因子 = 左子树高度 - 右子树高度
return height(node.left) - height(node.right);
}
/* 右旋操作 */
TreeNode? rightRotate(TreeNode? node)
{
TreeNode? child = node.left;
TreeNode? grandChild = child?.right;
// child 为原点 node 向右旋转
child.right = node;
node.left = grandChild;
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根结点
return child;
}
/* 左旋操作 */
TreeNode? leftRotate(TreeNode? node)
{
TreeNode? child = node.right;
TreeNode? grandChild = child?.left;
// child 为原点 node 向左旋转
child.left = node;
node.right = grandChild;
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根结点
return child;
}
/* 执行旋转操作,使该子树重新恢复平衡 */
TreeNode? rotate(TreeNode? node)
{
// 获取结点 node 的平衡因子
int balanceFactorInt = balanceFactor(node);
// 左偏树
if (balanceFactorInt > 1)
{ {
tree.insert(val); if (balanceFactor(node.left) >= 0)
Console.WriteLine("\n插入结点 " + val + "AVL 树为"); {
PrintUtil.PrintTree(tree.root); // 右旋
return rightRotate(node);
}
else
{
// 先左旋后右旋
node.left = leftRotate(node?.left);
return rightRotate(node);
}
} }
// 右偏树
static void testRemove(AVLTree tree, int val) if (balanceFactorInt < -1)
{ {
tree.remove(val); if (balanceFactor(node.right) <= 0)
Console.WriteLine("\n删除结点 " + val + "AVL 树为"); {
PrintUtil.PrintTree(tree.root); // 左旋
return leftRotate(node);
}
else
{
// 先右旋后左旋
node.right = rightRotate(node?.right);
return leftRotate(node);
}
} }
// 平衡树无需旋转直接返回
return node;
}
[Test] /* 插入结点 */
public void Test() public TreeNode? insert(int val)
{
root = insertHelper(root, val);
return root;
}
/* 递归插入结点(辅助函数) */
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);
else if (val > node.val)
node.right = insertHelper(node.right, val);
else
return node; // 重复结点不插入直接返回
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根结点
return node;
}
/* 删除结点 */
public TreeNode? remove(int val)
{
root = removeHelper(root, val);
return root;
}
/* 递归删除结点(辅助函数) */
private TreeNode? removeHelper(TreeNode? node, int val)
{
if (node == null) return null;
/* 1. 查找结点,并删除之 */
if (val < node.val)
node.left = removeHelper(node.left, val);
else if (val > node.val)
node.right = removeHelper(node.right, val);
else
{ {
/* 初始化空 AVL 树 */ if (node.left == null || node.right == null)
AVLTree avlTree = new AVLTree(); {
TreeNode? child = node.left != null ? node.left : node.right;
/* 插入结点 */ // 子结点数量 = 0 直接删除 node 并返回
// 请关注插入结点后AVL 树是如何保持平衡的 if (child == null)
testInsert(avlTree, 1); return null;
testInsert(avlTree, 2); // 子结点数量 = 1 直接删除 node
testInsert(avlTree, 3); else
testInsert(avlTree, 4); node = child;
testInsert(avlTree, 5); }
testInsert(avlTree, 8); else
testInsert(avlTree, 7); {
testInsert(avlTree, 9); // 子结点数量 = 2 则将中序遍历的下个结点删除并用该结点替换当前结点
testInsert(avlTree, 10); TreeNode? temp = getInOrderNext(node.right);
testInsert(avlTree, 6); node.right = removeHelper(node.right, temp.val);
node.val = temp.val;
/* 插入重复结点 */ }
testInsert(avlTree, 7);
/* 删除结点 */
// 请关注删除结点后AVL 树是如何保持平衡的
testRemove(avlTree, 8); // 删除度为 0 的结点
testRemove(avlTree, 5); // 删除度为 1 的结点
testRemove(avlTree, 4); // 删除度为 2 的结点
/* 查询结点 */
TreeNode? node = avlTree.search(7);
Console.WriteLine("\n查找到的结点对象为 " + node + ",结点值 = " + node?.val);
} }
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根结点
return node;
}
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
private TreeNode? getInOrderNext(TreeNode? node)
{
if (node == null) return node;
// 循环访问左子结点直到叶结点时为最小结点跳出
while (node.left != null)
{
node = node.left;
}
return node;
}
/* 查找结点 */
public TreeNode? search(int val)
{
TreeNode? cur = root;
// 循环查找越过叶结点后跳出
while (cur != null)
{
// 目标结点在 cur 的右子树中
if (cur.val < val)
cur = cur.right;
// 目标结点在 cur 的左子树中
else if (cur.val > val)
cur = cur.left;
// 找到目标结点跳出循环
else
break;
}
// 返回目标结点
return cur;
}
}
public class avl_tree
{
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);
Console.WriteLine("\n删除结点 " + val + "AVL 树为");
PrintUtil.PrintTree(tree.root);
}
[Test]
public void Test()
{
/* 初始化空 AVL 树 */
AVLTree avlTree = new AVLTree();
/* 插入结点 */
// 请关注插入结点后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, 7);
/* 删除结点 */
// 请关注删除结点后AVL 树是如何保持平衡的
testRemove(avlTree, 8); // 删除度为 0 的结点
testRemove(avlTree, 5); // 删除度为 1 的结点
testRemove(avlTree, 4); // 删除度为 2 的结点
/* 查询结点 */
TreeNode? node = avlTree.search(7);
Console.WriteLine("\n查找到的结点对象为 " + node + ",结点值 = " + node?.val);
} }
} }

View file

@ -7,176 +7,177 @@
using hello_algo.include; using hello_algo.include;
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_tree namespace hello_algo.chapter_tree;
class BinarySearchTree
{ {
class BinarySearchTree TreeNode? root;
public BinarySearchTree(int[] nums)
{ {
TreeNode? root; Array.Sort(nums); // 排序数组
root = buildTree(nums, 0, nums.Length - 1); // 构建二叉搜索树
}
public BinarySearchTree(int[] nums) { /* 获取二叉树根结点 */
Array.Sort(nums); // 排序数组 public TreeNode? getRoot()
root = buildTree(nums, 0, nums.Length - 1); // 构建二叉搜索树 {
} return root;
}
/* 获取二叉树根结点 */ /* 构建二叉搜索树 */
public TreeNode? getRoot() { public TreeNode? buildTree(int[] nums, int i, int j)
return root; {
} if (i > j) return null;
// 将数组中间结点作为根结点
int mid = (i + j) / 2;
TreeNode root = new TreeNode(nums[mid]);
// 递归建立左子树和右子树
root.left = buildTree(nums, i, mid - 1);
root.right = buildTree(nums, mid + 1, j);
return root;
}
/* 构建二叉搜索树 */ /* 查找结点 */
public TreeNode? buildTree(int[] nums, int i, int j) { public TreeNode? search(int num)
if (i > j) return null; {
// 将数组中间结点作为根结点 TreeNode? cur = root;
int mid = (i + j) / 2; // 循环查找越过叶结点后跳出
TreeNode root = new TreeNode(nums[mid]); while (cur != null)
// 递归建立左子树和右子树
root.left = buildTree(nums, i, mid - 1);
root.right = buildTree(nums, mid + 1, j);
return root;
}
/* 查找结点 */
public TreeNode? search(int num)
{ {
TreeNode? cur = root; // 目标结点在 cur 的右子树中
// 循环查找越过叶结点后跳出 if (cur.val < num) cur = cur.right;
while (cur != null) // 目标结点在 cur 的左子树中
{ else if (cur.val > num) cur = cur.left;
// 目标结点在 cur 的右子树中 // 找到目标结点跳出循环
if (cur.val < num) cur = cur.right; else break;
// 目标结点在 cur 的左子树中 }
else if (cur.val > num) cur = cur.left; // 返回目标结点
// 找到目标结点跳出循环 return cur;
else break; }
}
// 返回目标结点 /* 插入结点 */
return cur; public TreeNode? insert(int num)
{
// 若树为空直接提前返回
if (root == null) return null;
TreeNode? cur = root, pre = null;
// 循环查找越过叶结点后跳出
while (cur != null)
{
// 找到重复结点直接返回
if (cur.val == num) return null;
pre = cur;
// 插入位置在 cur 的右子树中
if (cur.val < num) cur = cur.right;
// 插入位置在 cur 的左子树中
else cur = cur.left;
} }
/* 插入结点 */ // 插入结点 val
public TreeNode? insert(int num) TreeNode node = new TreeNode(num);
if (pre != null)
{ {
// 若树为空直接提前返回 if (pre.val < num) pre.right = node;
if (root == null) return null; else pre.left = node;
TreeNode? cur = root, pre = null;
// 循环查找越过叶结点后跳出
while (cur != null)
{
// 找到重复结点直接返回
if (cur.val == num) return null;
pre = cur;
// 插入位置在 cur 的右子树中
if (cur.val < num) cur = cur.right;
// 插入位置在 cur 的左子树中
else cur = cur.left;
}
// 插入结点 val
TreeNode node = new TreeNode(num);
if (pre != null)
{
if (pre.val < num) pre.right = node;
else pre.left = node;
}
return node;
} }
return node;
}
/* 删除结点 */ /* 删除结点 */
public TreeNode? remove(int num) public TreeNode? remove(int num)
{
// 若树为空直接提前返回
if (root == null) return null;
TreeNode? cur = root, pre = null;
// 循环查找越过叶结点后跳出
while (cur != null)
{ {
// 若树为空直接提前返回 // 找到待删除结点跳出循环
if (root == null) return null; if (cur.val == num) break;
TreeNode? cur = root, pre = null; pre = cur;
// 循环查找越过叶结点后跳出 // 待删除结点在 cur 的右子树中
while (cur != null) if (cur.val < num) cur = cur.right;
// 待删除结点在 cur 的左子树中
else cur = cur.left;
}
// 若无待删除结点则直接返回
if (cur == null || pre == null) return null;
// 子结点数量 = 0 or 1
if (cur.left == null || cur.right == null)
{
// 当子结点数量 = 0 / 1 child = null / 该子结点
TreeNode? child = cur.left != null ? cur.left : cur.right;
// 删除结点 cur
if (pre.left == cur)
{ {
// 找到待删除结点跳出循环 pre.left = child;
if (cur.val == num) break;
pre = cur;
// 待删除结点在 cur 的右子树中
if (cur.val < num) cur = cur.right;
// 待删除结点在 cur 的左子树中
else cur = cur.left;
} }
// 若无待删除结点则直接返回
if (cur == null || pre == null) return null;
// 子结点数量 = 0 or 1
if (cur.left == null || cur.right == null)
{
// 当子结点数量 = 0 / 1 child = null / 该子结点
TreeNode? child = cur.left != null ? cur.left : cur.right;
// 删除结点 cur
if (pre.left == cur)
{
pre.left = child;
}
else
{
pre.right = child;
}
}
// 子结点数量 = 2
else else
{ {
// 获取中序遍历中 cur 的下一个结点 pre.right = child;
TreeNode? nex = getInOrderNext(cur.right);
if (nex != null)
{
int tmp = nex.val;
// 递归删除结点 nex
remove(nex.val);
// nex 的值复制给 cur
cur.val = tmp;
}
} }
return cur;
} }
// 子结点数量 = 2
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */ else
private TreeNode? getInOrderNext(TreeNode? root)
{ {
if (root == null) return root; // 获取中序遍历中 cur 的下一个结点
// 循环访问左子结点直到叶结点时为最小结点跳出 TreeNode? nex = getInOrderNext(cur.right);
while (root.left != null) if (nex != null)
{ {
root = root.left; int tmp = nex.val;
// 递归删除结点 nex
remove(nex.val);
// nex 的值复制给 cur
cur.val = tmp;
} }
return root;
} }
return cur;
} }
public class binary_search_tree /* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
private TreeNode? getInOrderNext(TreeNode? root)
{ {
[Test] if (root == null) return root;
public void Test() // 循环访问左子结点直到叶结点时为最小结点跳出
while (root.left != null)
{ {
/* 初始化二叉搜索树 */ root = root.left;
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
BinarySearchTree bst = new BinarySearchTree(nums);
Console.WriteLine("\n初始化的二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
/* 查找结点 */
TreeNode? node = bst.search(7);
Console.WriteLine("\n查找到的结点对象为 " + node + ",结点值 = " + node.val);
/* 插入结点 */
node = bst.insert(16);
Console.WriteLine("\n插入结点 16 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
/* 删除结点 */
bst.remove(1);
Console.WriteLine("\n删除结点 1 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
bst.remove(2);
Console.WriteLine("\n删除结点 2 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
bst.remove(4);
Console.WriteLine("\n删除结点 4 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
} }
return root;
}
}
public class binary_search_tree
{
[Test]
public void Test()
{
/* 初始化二叉搜索树 */
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
BinarySearchTree bst = new BinarySearchTree(nums);
Console.WriteLine("\n初始化的二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
/* 查找结点 */
TreeNode? node = bst.search(7);
Console.WriteLine("\n查找到的结点对象为 " + node + ",结点值 = " + node.val);
/* 插入结点 */
node = bst.insert(16);
Console.WriteLine("\n插入结点 16 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
/* 删除结点 */
bst.remove(1);
Console.WriteLine("\n删除结点 1 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
bst.remove(2);
Console.WriteLine("\n删除结点 2 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
bst.remove(4);
Console.WriteLine("\n删除结点 4 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
} }
} }

View file

@ -7,40 +7,38 @@
using hello_algo.include; using hello_algo.include;
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_tree namespace hello_algo.chapter_tree;
public class binary_tree
{ {
[Test]
public class binary_tree public void Test()
{ {
[Test] /* 初始化二叉树 */
public void Test() // 初始化结点
{ TreeNode n1 = new TreeNode(1);
/* 初始化二叉树 */ TreeNode n2 = new TreeNode(2);
// 初始化结点 TreeNode n3 = new TreeNode(3);
TreeNode n1 = new TreeNode(1); TreeNode n4 = new TreeNode(4);
TreeNode n2 = new TreeNode(2); TreeNode n5 = new TreeNode(5);
TreeNode n3 = new TreeNode(3); // 构建引用指向即指针
TreeNode n4 = new TreeNode(4); n1.left = n2;
TreeNode n5 = new TreeNode(5); n1.right = n3;
// 构建引用指向即指针 n2.left = n4;
n1.left = n2; n2.right = n5;
n1.right = n3; Console.WriteLine("\n初始化二叉树\n");
n2.left = n4; PrintUtil.PrintTree(n1);
n2.right = n5;
Console.WriteLine("\n初始化二叉树\n");
PrintUtil.PrintTree(n1);
/* 插入与删除结点 */ /* 插入与删除结点 */
TreeNode P = new TreeNode(0); TreeNode P = new TreeNode(0);
// n1 -> n2 中间插入结点 P // n1 -> n2 中间插入结点 P
n1.left = P; n1.left = P;
P.left = n2; P.left = n2;
Console.WriteLine("\n插入结点 P 后\n"); Console.WriteLine("\n插入结点 P 后\n");
PrintUtil.PrintTree(n1); PrintUtil.PrintTree(n1);
// 删除结点 P // 删除结点 P
n1.left = n2; n1.left = n2;
Console.WriteLine("\n删除结点 P 后\n"); Console.WriteLine("\n删除结点 P 后\n");
PrintUtil.PrintTree(n1); PrintUtil.PrintTree(n1);
}
} }
} }

View file

@ -7,42 +7,41 @@
using hello_algo.include; using hello_algo.include;
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_tree namespace hello_algo.chapter_tree;
public class binary_tree_bfs
{ {
public class binary_tree_bfs
/* 层序遍历 */
public List<int> hierOrder(TreeNode root)
{ {
// 初始化队列加入根结点
/* 层序遍历 */ Queue<TreeNode> queue = new();
public List<int> hierOrder(TreeNode root) queue.Enqueue(root);
// 初始化一个列表用于保存遍历序列
List<int> list = new();
while (queue.Count != 0)
{ {
// 初始化队列加入根结点 TreeNode node = queue.Dequeue(); // 队列出队
Queue<TreeNode> queue = new(); list.Add(node.val); // 保存结点值
queue.Enqueue(root); if (node.left != null)
// 初始化一个列表用于保存遍历序列 queue.Enqueue(node.left); // 左子结点入队
List<int> list = new(); if (node.right != null)
while (queue.Count != 0) queue.Enqueue(node.right); // 右子结点入队
{
TreeNode node = queue.Dequeue(); // 队列出队
list.Add(node.val); // 保存结点值
if (node.left != null)
queue.Enqueue(node.left); // 左子结点入队
if (node.right != null)
queue.Enqueue(node.right); // 右子结点入队
}
return list;
} }
return list;
}
[Test] [Test]
public void Test() public void Test()
{ {
/* 初始化二叉树 */ /* 初始化二叉树 */
// 这里借助了一个从数组直接生成二叉树的函数 // 这里借助了一个从数组直接生成二叉树的函数
TreeNode? root = TreeNode.ArrToTree(new int?[] { 1, 2, 3, 4, 5, 6, 7 }); TreeNode? root = TreeNode.ArrToTree(new int?[] { 1, 2, 3, 4, 5, 6, 7 });
Console.WriteLine("\n初始化二叉树\n"); Console.WriteLine("\n初始化二叉树\n");
PrintUtil.PrintTree(root); PrintUtil.PrintTree(root);
List<int> list = hierOrder(root); List<int> list = hierOrder(root);
Console.WriteLine("\n层序遍历的结点打印序列 = " + string.Join(",", list.ToArray())); Console.WriteLine("\n层序遍历的结点打印序列 = " + string.Join(",", list.ToArray()));
}
} }
} }

View file

@ -7,62 +7,61 @@
using hello_algo.include; using hello_algo.include;
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_tree namespace hello_algo.chapter_tree;
public class binary_tree_dfs
{ {
public class binary_tree_dfs List<int> list = new();
/* 前序遍历 */
void preOrder(TreeNode? root)
{ {
List<int> list = new(); if (root == null) return;
// 访问优先级根结点 -> 左子树 -> 右子树
list.Add(root.val);
preOrder(root.left);
preOrder(root.right);
}
/* 前序遍历 */ /* 中序遍历 */
void preOrder(TreeNode? root) void inOrder(TreeNode? root)
{ {
if (root == null) return; if (root == null) return;
// 访问优先级根结点 -> 左子树 -> 右子树 // 访问优先级左子树 -> 根结点 -> 右子树
list.Add(root.val); inOrder(root.left);
preOrder(root.left); list.Add(root.val);
preOrder(root.right); inOrder(root.right);
} }
/* 中序遍历 */ /* 后序遍历 */
void inOrder(TreeNode? root) void postOrder(TreeNode? root)
{ {
if (root == null) return; if (root == null) return;
// 访问优先级左子树 -> 根结点 -> 右子树 // 访问优先级左子树 -> 右子树 -> 根结点
inOrder(root.left); postOrder(root.left);
list.Add(root.val); postOrder(root.right);
inOrder(root.right); list.Add(root.val);
} }
/* 后序遍历 */ [Test]
void postOrder(TreeNode? root) public void Test()
{ {
if (root == null) return; /* 初始化二叉树 */
// 访问优先级左子树 -> 右子树 -> 根结点 // 这里借助了一个从数组直接生成二叉树的函数
postOrder(root.left); TreeNode? root = TreeNode.ArrToTree(new int?[] { 1, 2, 3, 4, 5, 6, 7 });
postOrder(root.right); Console.WriteLine("\n初始化二叉树\n");
list.Add(root.val); PrintUtil.PrintTree(root);
}
[Test] list.Clear();
public void Test() preOrder(root);
{ Console.WriteLine("\n前序遍历的结点打印序列 = " + string.Join(",", list.ToArray()));
/* 初始化二叉树 */
// 这里借助了一个从数组直接生成二叉树的函数
TreeNode? root = TreeNode.ArrToTree(new int?[] { 1, 2, 3, 4, 5, 6, 7 });
Console.WriteLine("\n初始化二叉树\n");
PrintUtil.PrintTree(root);
list.Clear(); list.Clear();
preOrder(root); inOrder(root);
Console.WriteLine("\n序遍历的结点打印序列 = " + string.Join(",", list.ToArray())); Console.WriteLine("\n中序遍历的结点打印序列 = " + string.Join(",", list.ToArray()));
list.Clear(); list.Clear();
inOrder(root); postOrder(root);
Console.WriteLine("\n中序遍历的结点打印序列 = " + string.Join(",", list.ToArray())); Console.WriteLine("\n后序遍历的结点打印序列 = " + string.Join(",", list.ToArray()));
list.Clear();
postOrder(root);
Console.WriteLine("\n后序遍历的结点打印序列 = " + string.Join(",", list.ToArray()));
}
} }
} }

View file

@ -11,8 +11,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="NUnit" Version="3.13.3" /> <PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" /> <PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
<PackageReference Include="coverlet.collector" Version="3.1.0" /> <PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -2,67 +2,66 @@
// Created Time: 2022-12-16 // Created Time: 2022-12-16
// Author: mingXta (1195669834@qq.com) // Author: mingXta (1195669834@qq.com)
namespace hello_algo.include namespace hello_algo.include;
/// <summary>
/// Definition for a singly-linked list node
/// </summary>
public class ListNode
{ {
public int val;
public ListNode? next;
/// <summary> /// <summary>
/// Definition for a singly-linked list node /// Generate a linked list with an array
/// </summary> /// </summary>
public class ListNode /// <param name="x"></param>
public ListNode(int x)
{ {
public int val; val = x;
public ListNode? next;
/// <summary>
/// Generate a linked list with an array
/// </summary>
/// <param name="x"></param>
public ListNode(int x)
{
val = x;
}
/// <summary>
/// Generate a linked list with an array
/// </summary>
/// <param name="arr"></param>
/// <returns></returns>
public static ListNode? ArrToLinkedList(int[] arr)
{
ListNode dum = new ListNode(0);
ListNode head = dum;
foreach (int val in arr)
{
head.next = new ListNode(val);
head = head.next;
}
return dum.next;
}
/// <summary>
/// Get a list node with specific value from a linked list
/// </summary>
/// <param name="head"></param>
/// <param name="val"></param>
/// <returns></returns>
public static ListNode? GetListNode(ListNode? head, int val)
{
while (head != null && head.val != val)
{
head = head.next;
}
return head;
}
public override string? ToString()
{
List<string> list = new();
var head = this;
while (head != null)
{
list.Add(head.val.ToString());
head = head.next;
}
return string.Join("->", list);
}
} }
}
/// <summary>
/// Generate a linked list with an array
/// </summary>
/// <param name="arr"></param>
/// <returns></returns>
public static ListNode? ArrToLinkedList(int[] arr)
{
ListNode dum = new ListNode(0);
ListNode head = dum;
foreach (int val in arr)
{
head.next = new ListNode(val);
head = head.next;
}
return dum.next;
}
/// <summary>
/// Get a list node with specific value from a linked list
/// </summary>
/// <param name="head"></param>
/// <param name="val"></param>
/// <returns></returns>
public static ListNode? GetListNode(ListNode? head, int val)
{
while (head != null && head.val != val)
{
head = head.next;
}
return head;
}
public override string? ToString()
{
List<string> list = new();
var head = this;
while (head != null)
{
list.Add(head.val.ToString());
head = head.next;
}
return string.Join("->", list);
}
}

View file

@ -4,121 +4,119 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
namespace hello_algo.include namespace hello_algo.include;
public class Trunk
{ {
public class Trunk public Trunk? prev;
public String str;
public Trunk(Trunk? prev, String str)
{ {
public Trunk? prev; this.prev = prev;
public String str; this.str = str;
}
};
public Trunk(Trunk? prev, String str) public class PrintUtil
{ {
this.prev = prev; /**
this.str = str; * Print a linked list
} * @param head
}; */
public static void PrintLinkedList(ListNode head)
public class PrintUtil
{ {
/** List<String> list = new();
* Print a linked list while (head != null)
* @param head
*/
public static void PrintLinkedList(ListNode head)
{ {
List<String> list = new(); list.Add(head.val.ToString());
while (head != null) head = head.next;
{
list.Add(head.val.ToString());
head = head.next;
}
Console.Write(String.Join(" -> ", list));
}
/**
* The interface of the tree printer
* This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
* @param root
*/
public static void PrintTree(TreeNode? root)
{
PrintTree(root, null, false);
}
/**
* Print a binary tree
* @param root
* @param prev
* @param isLeft
*/
public static void PrintTree(TreeNode? root, Trunk? prev, bool isLeft)
{
if (root == null)
{
return;
}
String prev_str = " ";
Trunk trunk = new Trunk(prev, prev_str);
PrintTree(root.right, trunk, true);
if (prev == null)
{
trunk.str = "———";
}
else if (isLeft)
{
trunk.str = "/———";
prev_str = " |";
}
else
{
trunk.str = "\\———";
prev.str = prev_str;
}
showTrunks(trunk);
Console.WriteLine(" " + root.val);
if (prev != null)
{
prev.str = prev_str;
}
trunk.str = " |";
PrintTree(root.left, trunk, false);
}
/**
* Helper function to print branches of the binary tree
* @param p
*/
public static void showTrunks(Trunk? p)
{
if (p == null)
{
return;
}
showTrunks(p.prev);
Console.Write(p.str);
}
/**
* Print a hash map
* @param <K>
* @param <V>
* @param map
*/
public static void printHashMap<K, V>(Dictionary<K, V> map) where K : notnull
{
foreach (var kv in map.Keys)
{
Console.WriteLine(kv.ToString() + " -> " + map[kv]?.ToString());
}
} }
Console.Write(String.Join(" -> ", list));
} }
/**
* The interface of the tree printer
* This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
* @param root
*/
public static void PrintTree(TreeNode? root)
{
PrintTree(root, null, false);
}
/**
* Print a binary tree
* @param root
* @param prev
* @param isLeft
*/
public static void PrintTree(TreeNode? root, Trunk? prev, bool isLeft)
{
if (root == null)
{
return;
}
String prev_str = " ";
Trunk trunk = new Trunk(prev, prev_str);
PrintTree(root.right, trunk, true);
if (prev == null)
{
trunk.str = "———";
}
else if (isLeft)
{
trunk.str = "/———";
prev_str = " |";
}
else
{
trunk.str = "\\———";
prev.str = prev_str;
}
showTrunks(trunk);
Console.WriteLine(" " + root.val);
if (prev != null)
{
prev.str = prev_str;
}
trunk.str = " |";
PrintTree(root.left, trunk, false);
}
/**
* Helper function to print branches of the binary tree
* @param p
*/
public static void showTrunks(Trunk? p)
{
if (p == null)
{
return;
}
showTrunks(p.prev);
Console.Write(p.str);
}
/**
* Print a hash map
* @param <K>
* @param <V>
* @param map
*/
public static void printHashMap<K, V>(Dictionary<K, V> map) where K : notnull
{
foreach (var kv in map.Keys)
{
Console.WriteLine(kv.ToString() + " -> " + map[kv]?.ToString());
}
}
} }

View file

@ -4,95 +4,94 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
namespace hello_algo.include namespace hello_algo.include;
public class TreeNode
{ {
public class TreeNode public int val; // 结点值
public int height; // 结点高度
public TreeNode? left; // 左子结点引用
public TreeNode? right; // 右子结点引用
public TreeNode(int x)
{ {
public int val; // 结点值 val = x;
public int height; // 结点高度 }
public TreeNode? left; // 左子结点引用
public TreeNode? right; // 右子结点引用
public TreeNode(int x) /**
* Generate a binary tree given an array
* @param arr
* @return
*/
public static TreeNode? ArrToTree(int?[] arr)
{
if (arr.Length == 0 || arr[0] == null)
return null;
TreeNode root = new TreeNode((int)arr[0]);
Queue<TreeNode> queue = new Queue<TreeNode>();
queue.Enqueue(root);
int i = 0;
while (queue.Count != 0)
{ {
val = x; TreeNode node = queue.Dequeue();
} if (++i >= arr.Length) break;
if (arr[i] != null)
/**
* Generate a binary tree given an array
* @param arr
* @return
*/
public static TreeNode? ArrToTree(int?[] arr)
{
if (arr.Length == 0 || arr[0] == null)
return null;
TreeNode root = new TreeNode((int) arr[0]);
Queue<TreeNode> queue = new Queue<TreeNode>();
queue.Enqueue(root);
int i = 0;
while (queue.Count != 0)
{ {
TreeNode node = queue.Dequeue(); node.left = new TreeNode((int)arr[i]);
if (++i >= arr.Length) break; queue.Enqueue(node.left);
if (arr[i] != null)
{
node.left = new TreeNode((int) arr[i]);
queue.Enqueue(node.left);
}
if (++i >= arr.Length) break;
if (arr[i] != null)
{
node.right = new TreeNode((int) arr[i]);
queue.Enqueue(node.right);
}
} }
if (++i >= arr.Length) break;
if (arr[i] != null)
{
node.right = new TreeNode((int)arr[i]);
queue.Enqueue(node.right);
}
}
return root;
}
/**
* Serialize a binary tree to a list
* @param root
* @return
*/
public static List<int?> TreeToList(TreeNode root)
{
List<int?> list = new();
if (root == null) return list;
Queue<TreeNode?> queue = new();
while (queue.Count != 0)
{
TreeNode? node = queue.Dequeue();
if (node != null)
{
list.Add(node.val);
queue.Enqueue(node.left);
queue.Enqueue(node.right);
}
else
{
list.Add(null);
}
}
return list;
}
/**
* Get a tree node with specific value in a binary tree
* @param root
* @param val
* @return
*/
public static TreeNode? GetTreeNode(TreeNode? root, int val)
{
if (root == null)
return null;
if (root.val == val)
return root; return root;
} TreeNode? left = GetTreeNode(root.left, val);
TreeNode? right = GetTreeNode(root.right, val);
/** return left != null ? left : right;
* Serialize a binary tree to a list
* @param root
* @return
*/
public static List<int?> TreeToList(TreeNode root)
{
List<int?> list = new();
if (root == null) return list;
Queue<TreeNode?> queue = new();
while (queue.Count != 0)
{
TreeNode? node = queue.Dequeue();
if (node != null)
{
list.Add(node.val);
queue.Enqueue(node.left);
queue.Enqueue(node.right);
}
else
{
list.Add(null);
}
}
return list;
}
/**
* Get a tree node with specific value in a binary tree
* @param root
* @param val
* @return
*/
public static TreeNode? GetTreeNode(TreeNode? root, int val)
{
if (root == null)
return null;
if (root.val == val)
return root;
TreeNode? left = GetTreeNode(root.left, val);
TreeNode? right = GetTreeNode(root.right, val);
return left != null ? left : right;
}
} }
} }

View file

@ -166,16 +166,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C#" === "C#"
```csharp title="array.cs" ```csharp title="array.cs"
/* 随机返回一个数组元素 */ [class]{array}-[func]{randomAccess}
int RandomAccess(int[] nums)
{
Random random=new();
// 在区间 [0, nums.Length) 中随机抽取一个数字
int randomIndex = random.Next(nums.Length);
// 获取并返回随机元素
int randomNum = nums[randomIndex];
return randomNum;
}
``` ```
=== "Swift" === "Swift"
@ -256,19 +247,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C#" === "C#"
```csharp title="array.cs" ```csharp title="array.cs"
/* 扩展数组长度 */ [class]{array}-[func]{extend}
int[] Extend(int[] nums, int enlarge)
{
// 初始化一个扩展长度后的数组
int[] res = new int[nums.Length + enlarge];
// 将原数组中的所有元素复制到新数组
for (int i = 0; i < nums.Length; i++)
{
res[i] = nums[i];
}
// 返回扩展后的新数组
return res;
}
``` ```
=== "Swift" === "Swift"
@ -373,26 +352,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C#" === "C#"
```csharp title="array.cs" ```csharp title="array.cs"
/* 在数组的索引 index 处插入元素 num */ [class]{array}-[func]{insert}
void Insert(int[] nums, int num, int index)
{ [class]{array}-[func]{remove}
// 把索引 index 以及之后的所有元素向后移动一位
for (int i = nums.Length - 1; i > index; i--)
{
nums[i] = nums[i - 1];
}
// 将 num 赋给 index 处元素
nums[index] = num;
}
/* 删除索引 index 处元素 */
void Remove(int[] nums, int index)
{
// 把索引 index 之后的所有元素向前移动一位
for (int i = index; i < nums.Length - 1; i++)
{
nums[i] = nums[i + 1];
}
}
``` ```
=== "Swift" === "Swift"
@ -487,21 +449,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C#" === "C#"
```csharp title="array.cs" ```csharp title="array.cs"
/* 遍历数组 */ [class]{array}-[func]{traverse}
void Traverse(int[] nums)
{
int count = 0;
// 通过索引遍历数组
for (int i = 0; i < nums.Length; i++)
{
count++;
}
// 直接遍历数组
foreach (int num in nums)
{
count++;
}
}
``` ```
=== "Swift" === "Swift"
@ -586,16 +534,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C#" === "C#"
```csharp title="array.cs" ```csharp title="array.cs"
/* 在数组中查找指定元素 */ [class]{array}-[func]{find}
int Find(int[] nums, int target)
{
for (int i = 0; i < nums.Length; i++)
{
if (nums[i] == target)
return i;
}
return -1;
}
``` ```
=== "Swift" === "Swift"

View file

@ -435,24 +435,9 @@ comments: true
=== "C#" === "C#"
```csharp title="linked_list.cs" ```csharp title="linked_list.cs"
// 在链表的结点 n0 之后插入结点 P [class]{linked_list}-[func]{insert}
void Insert(ListNode n0, ListNode P)
{
ListNode n1 = n0.next;
n0.next = P;
P.next = n1;
}
// 删除链表的结点 n0 之后的首个结点 [class]{linked_list}-[func]{remove}
void Remove(ListNode n0)
{
if (n0.next == null)
return;
// n0 -> P -> n1
ListNode P = n0.next;
ListNode n1 = P.next;
n0.next = n1;
}
``` ```
=== "Swift" === "Swift"
@ -556,17 +541,7 @@ comments: true
=== "C#" === "C#"
```csharp title="linked_list.cs" ```csharp title="linked_list.cs"
// 访问链表中索引为 index 的结点 [class]{linked_list}-[func]{access}
ListNode Access(ListNode head, int index)
{
for (int i = 0; i < index; i++)
{
if (head == null)
return null;
head = head.next;
}
return head;
}
``` ```
=== "Swift" === "Swift"
@ -652,19 +627,7 @@ comments: true
=== "C#" === "C#"
```csharp title="linked_list.cs" ```csharp title="linked_list.cs"
// 在链表中查找值为 target 的首个结点 [class]{linked_list}-[func]{find}
int Find(ListNode head, int target)
{
int index = 0;
while (head != null)
{
if (head.val == target)
return index;
head = head.next;
index++;
}
return -1;
}
``` ```
=== "Swift" === "Swift"

View file

@ -854,103 +854,7 @@ comments: true
=== "C#" === "C#"
```csharp title="my_list.cs" ```csharp title="my_list.cs"
class MyList [class]{MyList}-[func]{}
{
private int[] nums; // 数组(存储列表元素)
private int capacity = 10; // 列表容量
private int size = 0; // 列表长度(即当前元素数量)
private int extendRatio = 2; // 每次列表扩容的倍数
/* 构造函数 */
public MyList()
{
nums = new int[capacity];
}
/* 获取列表长度(即当前元素数量)*/
public int Size()
{
return size;
}
/* 获取列表容量 */
public int Capacity()
{
return capacity;
}
/* 访问元素 */
public int Get(int index)
{
// 索引如果越界则抛出异常,下同
if (index < 0 || index >= size)
throw new IndexOutOfRangeException("索引越界");
return nums[index];
}
/* 更新元素 */
public void Set(int index, int num)
{
if (index < 0 || index >= size)
throw new IndexOutOfRangeException("索引越界");
nums[index] = num;
}
/* 尾部添加元素 */
public void Add(int num)
{
// 元素数量超出容量时,触发扩容机制
if (size == Capacity())
ExtendCapacity();
nums[size] = num;
// 更新元素数量
size++;
}
/* 中间插入元素 */
public void Insert(int index, int num)
{
if (index < 0 || index >= size)
throw new IndexOutOfRangeException("索引越界");
// 元素数量超出容量时,触发扩容机制
if (size == Capacity())
ExtendCapacity();
// 将索引 index 以及之后的元素都向后移动一位
for (int j = size - 1; j >= index; j--)
{
nums[j + 1] = nums[j];
}
nums[index] = num;
// 更新元素数量
size++;
}
/* 删除元素 */
public int Remove(int index)
{
if (index < 0 || index >= size)
throw new IndexOutOfRangeException("索引越界");
int num = nums[index];
// 将索引 index 之后的元素都向前移动一位
for (int j = index; j < size - 1; j++)
{
nums[j] = nums[j + 1];
}
// 更新元素数量
size--;
// 返回被删除元素
return num;
}
/* 列表扩容 */
public void ExtendCapacity()
{
// 新建一个长度为 size 的数组,并将原数组拷贝到新数组
System.Array.Resize(ref nums, Capacity() * extendRatio);
// 更新列表容量
capacity = nums.Length;
}
}
``` ```
=== "Swift" === "Swift"

View file

@ -641,25 +641,7 @@ $$
=== "C#" === "C#"
```csharp title="space_complexity.cs" ```csharp title="space_complexity.cs"
/* 常数阶 */ [class]{space_complexity}-[func]{constant}
void constant(int n)
{
// 常量、变量、对象占用 O(1) 空间
int a = 0;
int b = 0;
int[] nums = new int[10000];
ListNode node = new ListNode(0);
// 循环中的变量占用 O(1) 空间
for (int i = 0; i < n; i++)
{
int c = 0;
}
// 循环中的函数占用 O(1) 空间
for (int i = 0; i < n; i++)
{
function();
}
}
``` ```
=== "Swift" === "Swift"
@ -759,24 +741,7 @@ $$
=== "C#" === "C#"
```csharp title="space_complexity.cs" ```csharp title="space_complexity.cs"
/* 线性阶 */ [class]{space_complexity}-[func]{linear}
void linear(int n)
{
// 长度为 n 的数组占用 O(n) 空间
int[] nums = new int[n];
// 长度为 n 的列表占用 O(n) 空间
List<ListNode> nodes = new();
for (int i = 0; i < n; i++)
{
nodes.Add(new ListNode(i));
}
// 长度为 n 的哈希表占用 O(n) 空间
Dictionary<int, String> map = new();
for (int i = 0; i < n; i++)
{
map.Add(i, i.ToString());
}
}
``` ```
=== "Swift" === "Swift"
@ -866,13 +831,7 @@ $$
=== "C#" === "C#"
```csharp title="space_complexity.cs" ```csharp title="space_complexity.cs"
/* 线性阶(递归实现) */ [class]{space_complexity}-[func]{linearRecur}
void linearRecur(int n)
{
Console.WriteLine("递归 n = " + n);
if (n == 1) return;
linearRecur(n - 1);
}
``` ```
=== "Swift" === "Swift"
@ -952,23 +911,7 @@ $$
=== "C#" === "C#"
```csharp title="space_complexity.cs" ```csharp title="space_complexity.cs"
/* 平方阶 */ [class]{space_complexity}-[func]{quadratic}
void quadratic(int n)
{
// 矩阵占用 O(n^2) 空间
int[,] numMatrix = new int[n, n];
// 二维列表占用 O(n^2) 空间
List<List<int>> numList = new();
for (int i = 0; i < n; i++)
{
List<int> tmp = new();
for (int j = 0; j < n; j++)
{
tmp.Add(0);
}
numList.Add(tmp);
}
}
``` ```
=== "Swift" === "Swift"
@ -1053,14 +996,7 @@ $$
=== "C#" === "C#"
```csharp title="space_complexity.cs" ```csharp title="space_complexity.cs"
/* 平方阶(递归实现) */ [class]{space_complexity}-[func]{quadraticRecur}
int quadraticRecur(int n)
{
if (n <= 0) return 0;
// 数组 nums 长度为 n, n-1, ..., 2, 1
int[] nums = new int[n];
return quadraticRecur(n - 1);
}
``` ```
=== "Swift" === "Swift"
@ -1143,15 +1079,7 @@ $$
=== "C#" === "C#"
```csharp title="space_complexity.cs" ```csharp title="space_complexity.cs"
/* 指数阶(建立满二叉树) */ [class]{space_complexity}-[func]{buildTree}
TreeNode? buildTree(int n)
{
if (n == 0) return null;
TreeNode root = new TreeNode(0);
root.left = buildTree(n - 1);
root.right = buildTree(n - 1);
return root;
}
``` ```
=== "Swift" === "Swift"

View file

@ -86,23 +86,7 @@ comments: true
=== "C#" === "C#"
```csharp title="leetcode_two_sum.cs" ```csharp title="leetcode_two_sum.cs"
class SolutionBruteForce [class]{leetcode_two_sum}-[func]{twoSumBruteForce}
{
public int[] twoSum(int[] nums, int target)
{
int size = nums.Length;
// 两层循环,时间复杂度 O(n^2)
for (int i = 0; i < size - 1; i++)
{
for (int j = i + 1; j < size; j++)
{
if (nums[i] + nums[j] == target)
return new int[] { i, j };
}
}
return new int[0];
}
}
``` ```
=== "Swift" === "Swift"
@ -195,25 +179,7 @@ comments: true
=== "C#" === "C#"
```csharp title="leetcode_two_sum.cs" ```csharp title="leetcode_two_sum.cs"
class SolutionHashMap [class]{leetcode_two_sum}-[func]{twoSumHashTable}
{
public int[] twoSum(int[] nums, int target)
{
int size = nums.Length;
// 辅助哈希表,空间复杂度 O(n)
Dictionary<int, int> dic = new();
// 单层循环,时间复杂度 O(n)
for (int i = 0; i < size; i++)
{
if (dic.ContainsKey(target - nums[i]))
{
return new int[] { dic[target - nums[i]], i };
}
dic.Add(nums[i], i);
}
return new int[0];
}
}
``` ```
=== "Swift" === "Swift"

View file

@ -854,15 +854,7 @@ $$
=== "C#" === "C#"
```csharp title="time_complexity.cs" ```csharp title="time_complexity.cs"
/* 常数阶 */ [class]{time_complexity}-[func]{constant}
int constant(int n)
{
int count = 0;
int size = 100000;
for (int i = 0; i < size; i++)
count++;
return count;
}
``` ```
=== "Swift" === "Swift"
@ -950,14 +942,7 @@ $$
=== "C#" === "C#"
```csharp title="time_complexity.cs" ```csharp title="time_complexity.cs"
/* 线性阶 */ [class]{time_complexity}-[func]{linear}
int linear(int n)
{
int count = 0;
for (int i = 0; i < n; i++)
count++;
return count;
}
``` ```
=== "Swift" === "Swift"
@ -1047,17 +1032,7 @@ $$
=== "C#" === "C#"
```csharp title="time_complexity.cs" ```csharp title="time_complexity.cs"
/* 线性阶(遍历数组) */ [class]{time_complexity}-[func]{arrayTraversal}
int arrayTraversal(int[] nums)
{
int count = 0;
// 循环次数与数组长度成正比
foreach(int num in nums)
{
count++;
}
return count;
}
``` ```
=== "Swift" === "Swift"
@ -1149,20 +1124,7 @@ $$
=== "C#" === "C#"
```csharp title="time_complexity.cs" ```csharp title="time_complexity.cs"
/* 平方阶 */ [class]{time_complexity}-[func]{quadratic}
int quadratic(int n)
{
int count = 0;
// 循环次数与数组长度成平方关系
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
count++;
}
}
return count;
}
``` ```
=== "Swift" === "Swift"
@ -1280,29 +1242,7 @@ $$
=== "C#" === "C#"
```csharp title="time_complexity.cs" ```csharp title="time_complexity.cs"
/* 平方阶(冒泡排序) */ [class]{time_complexity}-[func]{bubbleSort}
int bubbleSort(int[] nums)
{
int count = 0; // 计数器
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
for (int i = nums.Length - 1; 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;
count += 3; // 元素交换包含 3 个单元操作
}
}
}
return count;
}
``` ```
=== "Swift" === "Swift"
@ -1414,22 +1354,7 @@ $$
=== "C#" === "C#"
```csharp title="time_complexity.cs" ```csharp title="time_complexity.cs"
/* 指数阶(循环实现) */ [class]{time_complexity}-[func]{exponential}
int exponential(int n)
{
int count = 0, bas = 1;
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (int i = 0; i < n; i++)
{
for (int j = 0; j < bas; j++)
{
count++;
}
bas *= 2;
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count;
}
``` ```
=== "Swift" === "Swift"
@ -1520,12 +1445,7 @@ $$
=== "C#" === "C#"
```csharp title="time_complexity.cs" ```csharp title="time_complexity.cs"
/* 指数阶(递归实现) */ [class]{time_complexity}-[func]{expRecur}
int expRecur(int n)
{
if (n == 1) return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
``` ```
=== "Swift" === "Swift"
@ -1613,17 +1533,7 @@ $$
=== "C#" === "C#"
```csharp title="time_complexity.cs" ```csharp title="time_complexity.cs"
/* 对数阶(循环实现) */ [class]{time_complexity}-[func]{logarithmic}
int logarithmic(float n)
{
int count = 0;
while (n > 1)
{
n = n / 2;
count++;
}
return count;
}
``` ```
=== "Swift" === "Swift"
@ -1710,12 +1620,7 @@ $$
=== "C#" === "C#"
```csharp title="time_complexity.cs" ```csharp title="time_complexity.cs"
/* 对数阶(递归实现) */ [class]{time_complexity}-[func]{logRecur}
int logRecur(float n)
{
if (n <= 1) return 0;
return logRecur(n / 2) + 1;
}
``` ```
=== "Swift" === "Swift"
@ -1806,18 +1711,7 @@ $$
=== "C#" === "C#"
```csharp title="time_complexity.cs" ```csharp title="time_complexity.cs"
/* 线性对数阶 */ [class]{time_complexity}-[func]{linearLogRecur}
int linearLogRecur(float n)
{
if (n <= 1) return 1;
int count = linearLogRecur(n / 2) +
linearLogRecur(n / 2);
for (int i = 0; i < n; i++)
{
count++;
}
return count;
}
``` ```
=== "Swift" === "Swift"
@ -1921,18 +1815,7 @@ $$
=== "C#" === "C#"
```csharp title="time_complexity.cs" ```csharp title="time_complexity.cs"
/* 阶乘阶(递归实现) */ [class]{time_complexity}-[func]{factorialRecur}
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);
}
return count;
}
``` ```
=== "Swift" === "Swift"
@ -2146,40 +2029,9 @@ $$
=== "C#" === "C#"
```csharp title="worst_best_time_complexity.cs" ```csharp title="worst_best_time_complexity.cs"
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ [class]{worst_best_time_complexity}-[func]{randomNumbers}
int[] randomNumbers(int n)
{
int[] nums = new int[n];
// 生成数组 nums = { 1, 2, 3, ..., n }
for (int i = 0; i < n; i++)
{
nums[i] = i + 1;
}
// 随机打乱数组元素 [class]{worst_best_time_complexity}-[func]{findOne}
for (int i = 0; i < nums.Length; i++)
{
var index = new Random().Next(i, nums.Length);
var tmp = nums[i];
var ran = nums[index];
nums[i] = ran;
nums[index] = tmp;
}
return nums;
}
/* 查找数组 nums 中数字 1 所在索引 */
int findOne(int[] nums)
{
for (int i = 0; i < nums.Length; i++)
{
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
if (nums[i] == 1)
return i;
}
return -1;
}
``` ```
=== "Swift" === "Swift"

View file

@ -160,7 +160,7 @@ comments: true
=== "C#" === "C#"
```csharp title="graph_adjacency_matrix.cs" ```csharp title="graph_adjacency_matrix.cs"
[class]{GraphAdjMat}-[func]{}
``` ```
=== "Swift" === "Swift"
@ -409,7 +409,9 @@ comments: true
=== "C#" === "C#"
```csharp title="graph_adjacency_list.cs" ```csharp title="graph_adjacency_list.cs"
[class]{Vertex}-[func]{}
[class]{GraphAdjList}-[func]{}
``` ```
=== "Swift" === "Swift"

View file

@ -513,60 +513,9 @@ $$
=== "C#" === "C#"
```csharp title="array_hash_map.cs" ```csharp title="array_hash_map.cs"
/* 键值对 int->String */ [class]{Entry}-[func]{}
class Entry
{ [class]{ArrayHashMap}-[func]{}
public int key;
public String val;
public Entry(int key, String val)
{
this.key = key;
this.val = val;
}
}
/* 基于数组简易实现的哈希表 */
class ArrayHashMap
{
private List<Entry?> bucket;
public ArrayHashMap()
{
// 初始化一个长度为 100 的桶(数组)
bucket = new ();
for (int i = 0; i < 100; i++)
{
bucket.Add(null);
}
}
/* 哈希函数 */
private int hashFunc(int key)
{
int index = key % 100;
return index;
}
/* 查询操作 */
public String? get(int key)
{
int index = hashFunc(key);
Entry? pair = bucket[index];
if (pair == null) return null;
return pair.val;
}
/* 添加操作 */
public void put(int key, String val)
{
Entry pair = new Entry(key, val);
int index = hashFunc(key);
bucket[index]=pair;
}
/* 删除操作 */
public void remove(int key)
{
int index = hashFunc(key);
// 置为 null ,代表删除
bucket[index]=null;
}
}
``` ```
=== "Swift" === "Swift"

View file

@ -344,7 +344,11 @@ comments: true
=== "C#" === "C#"
```csharp title="my_heap.cs" ```csharp title="my_heap.cs"
[class]{MaxHeap}-[func]{left}
[class]{MaxHeap}-[func]{right}
[class]{MaxHeap}-[func]{parent}
``` ```
=== "Swift" === "Swift"
@ -415,7 +419,7 @@ comments: true
=== "C#" === "C#"
```csharp title="my_heap.cs" ```csharp title="my_heap.cs"
[class]{MaxHeap}-[func]{peek}
``` ```
=== "Swift" === "Swift"
@ -531,7 +535,9 @@ comments: true
=== "C#" === "C#"
```csharp title="my_heap.cs" ```csharp title="my_heap.cs"
[class]{MaxHeap}-[func]{push}
[class]{MaxHeap}-[func]{siftUp}
``` ```
=== "Swift" === "Swift"
@ -682,7 +688,9 @@ comments: true
=== "C#" === "C#"
```csharp title="my_heap.cs" ```csharp title="my_heap.cs"
[class]{MaxHeap}-[func]{poll}
[class]{MaxHeap}-[func]{siftDown}
``` ```
=== "Swift" === "Swift"
@ -759,7 +767,7 @@ comments: true
=== "C#" === "C#"
```csharp title="my_heap.cs" ```csharp title="my_heap.cs"
[class]{MaxHeap}-[func]{MaxHeap}
``` ```
=== "Swift" === "Swift"

View file

@ -113,25 +113,7 @@ $$
=== "C#" === "C#"
```csharp title="binary_search.cs" ```csharp title="binary_search.cs"
/* 二分查找(双闭区间) */ [class]{binary_search}-[func]{binarySearch}
int binarySearch(int[] nums, int target)
{
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
int i = 0, j = nums.Length - 1;
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
while (i <= j)
{
int m = (i + j) / 2; // 计算中点索引 m
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j]
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
j = m - 1;
else // 找到目标元素,返回其索引
return m;
}
// 未找到目标元素,返回 -1
return -1;
}
``` ```
=== "Swift" === "Swift"
@ -212,25 +194,7 @@ $$
=== "C#" === "C#"
```csharp title="binary_search.cs" ```csharp title="binary_search.cs"
/* 二分查找(左闭右开) */ [class]{binary_search}-[func]{binarySearch1}
int binarySearch1(int[] nums, int target)
{
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
int i = 0, j = nums.Length;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
while (i < j)
{
int m = (i + j) / 2; // 计算中点索引 m
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j)
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
j = m;
else // 找到目标元素,返回其索引
return m;
}
// 未找到目标元素,返回 -1
return -1;
}
``` ```
=== "Swift" === "Swift"

View file

@ -70,13 +70,7 @@ comments: true
=== "C#" === "C#"
```csharp title="hashing_search.cs" ```csharp title="hashing_search.cs"
/* 哈希查找(数组) */ [class]{hashing_search}-[func]{hashingSearchArray}
int hashingSearchArray(Dictionary<int, int> map, int target)
{
// 哈希表的 key: 目标元素value: 索引
// 若哈希表中无此 key ,返回 -1
return map.GetValueOrDefault(target, -1);
}
``` ```
=== "Swift" === "Swift"
@ -149,14 +143,7 @@ comments: true
=== "C#" === "C#"
```csharp title="hashing_search.cs" ```csharp title="hashing_search.cs"
/* 哈希查找(链表) */ [class]{hashing_search}-[func]{hashingSearchLinkedList}
ListNode? hashingSearchLinkedList(Dictionary<int, ListNode> map, int target)
{
// 哈希表的 key: 目标结点值value: 结点对象
// 若哈希表中无此 key ,返回 null
return map.GetValueOrDefault(target);
}
``` ```
=== "Swift" === "Swift"

View file

@ -68,20 +68,7 @@ comments: true
=== "C#" === "C#"
```csharp title="linear_search.cs" ```csharp title="linear_search.cs"
/* 线性查找(数组) */ [class]{linear_search}-[func]{linearSearchArray}
int linearSearchArray(int[] nums, int target)
{
// 遍历数组
for (int i = 0; i < nums.Length; i++)
{
// 找到目标元素,返回其索引
if (nums[i] == target)
return i;
}
// 未找到目标元素,返回 -1
return -1;
}
``` ```
=== "Swift" === "Swift"
@ -155,20 +142,7 @@ comments: true
=== "C#" === "C#"
```csharp title="linear_search.cs" ```csharp title="linear_search.cs"
/* 线性查找(链表) */ [class]{linear_search}-[func]{linearSearchLinkedList}
ListNode? linearSearchLinkedList(ListNode head, int target)
{
// 遍历链表
while (head != null)
{
// 找到目标结点,返回之
if (head.val == target)
return head;
head = head.next;
}
// 未找到目标结点,返回 null
return null;
}
``` ```
=== "Swift" === "Swift"

View file

@ -120,25 +120,7 @@ comments: true
=== "C#" === "C#"
```csharp title="bubble_sort.cs" ```csharp title="bubble_sort.cs"
/* 冒泡排序 */ [class]{bubble_sort}-[func]{bubbleSort}
void bubbleSort(int[] nums)
{
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
for (int i = nums.Length - 1; 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;
}
}
}
}
``` ```
=== "Swift" === "Swift"
@ -252,28 +234,7 @@ comments: true
=== "C#" === "C#"
```csharp title="bubble_sort.cs" ```csharp title="bubble_sort.cs"
/* 冒泡排序(标志优化)*/ [class]{bubble_sort}-[func]{bubbleSortWithFlag}
void bubbleSortWithFlag(int[] nums)
{
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
for (int i = nums.Length - 1; i > 0; i--)
{
bool flag = false; // 初始化标志位
// 内循环:冒泡操作
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;
flag = true; // 记录交换元素
}
}
if (!flag) break; // 此轮冒泡未交换任何元素,直接跳出
}
}
``` ```
=== "Swift" === "Swift"

View file

@ -98,22 +98,7 @@ comments: true
=== "C#" === "C#"
```csharp title="insertion_sort.cs" ```csharp title="insertion_sort.cs"
/* 插入排序 */ [class]{insertion_sort}-[func]{insertionSort}
void insertionSort(int[] nums)
{
// 外循环base = nums[1], nums[2], ..., nums[n-1]
for (int i = 1; i < nums.Length; i++)
{
int bas = nums[i], j = i - 1;
// 内循环:将 base 插入到左边的正确位置
while (j >= 0 && nums[j] > bas)
{
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
j--;
}
nums[j + 1] = bas; // 2. 将 base 赋值到正确位置
}
}
``` ```
=== "Swift" === "Swift"

View file

@ -264,46 +264,9 @@ comments: true
=== "C#" === "C#"
```csharp title="merge_sort.cs" ```csharp title="merge_sort.cs"
/* 合并左子数组和右子数组 */ [class]{merge_sort}-[func]{merge}
// 左子数组区间 [left, mid]
// 右子数组区间 [mid + 1, right]
void merge(int[] nums, int left, int mid, int right)
{
// 初始化辅助数组
int[] tmp = nums[left..(right + 1)];
// 左子数组的起始索引和结束索引
int leftStart = left - left, leftEnd = mid - left;
// 右子数组的起始索引和结束索引
int rightStart = mid + 1 - left, rightEnd = right - left;
// i, j 分别指向左子数组、右子数组的首元素
int i = leftStart, j = rightStart;
// 通过覆盖原数组 nums 来合并左子数组和右子数组
for (int k = left; k <= right; k++)
{
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
if (i > leftEnd)
nums[k] = tmp[j++];
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
else if (j > rightEnd || tmp[i] <= tmp[j])
nums[k] = tmp[i++];
// 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
else
nums[k] = tmp[j++];
}
}
/* 归并排序 */ [class]{merge_sort}-[func]{mergeSort}
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); // 递归右子数组
// 合并阶段
merge(nums, left, mid, right);
}
``` ```
=== "Swift" === "Swift"

View file

@ -113,30 +113,9 @@ comments: true
=== "C#" === "C#"
```csharp title="quick_sort.cs" ```csharp title="quick_sort.cs"
/* 元素交换 */ [class]{QuickSort}-[func]{swap}
void swap(int[] nums, int i, int j)
{
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* 哨兵划分 */ [class]{QuickSort}-[func]{partition}
int partition(int[] nums, int left, int right)
{
// 以 nums[left] 作为基准数
int i = left, j = right;
while (i < j)
{
while (i < j && nums[j] >= nums[left])
j--; // 从右向左找首个小于基准数的元素
while (i < j && nums[i] <= nums[left])
i++; // 从左向右找首个大于基准数的元素
swap(nums, i, j); // 交换这两个元素
}
swap(nums, i, left); // 将基准数交换至两子数组的分界线
return i; // 返回基准数的索引
}
``` ```
=== "Swift" === "Swift"
@ -225,19 +204,7 @@ comments: true
=== "C#" === "C#"
```csharp title="quick_sort.cs" ```csharp title="quick_sort.cs"
/* 快速排序 */ [class]{QuickSort}-[func]{quickSort}
void quickSort(int[] nums, int left, int right)
{
// 子数组长度为 1 时终止递归
if (left >= right)
return;
// 哨兵划分
int pivot = partition(nums, left, right);
// 递归左子数组、右子数组
quickSort(nums, left, pivot - 1);
quickSort(nums, pivot + 1, right);
}
``` ```
=== "Swift" === "Swift"
@ -355,29 +322,9 @@ comments: true
=== "C#" === "C#"
```csharp title="quick_sort.cs" ```csharp title="quick_sort.cs"
/* 选取三个元素的中位数 */ [class]{QuickSortMedian}-[func]{medianThree}
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]))
return left;
else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))
return mid;
else
return right;
}
/* 哨兵划分(三数取中值) */ [class]{QuickSortMedian}-[func]{partition}
int partition(int[] nums, int left, int right)
{
// 选取三个候选元素的中位数
int med = medianThree(nums, left, (left + right) / 2, right);
// 将中位数交换至数组最左端
swap(nums, left, med);
// 以 nums[left] 作为基准数
// 下同省略...
}
``` ```
=== "Swift" === "Swift"
@ -460,27 +407,7 @@ comments: true
=== "C#" === "C#"
```csharp title="quick_sort.cs" ```csharp title="quick_sort.cs"
/* 快速排序(尾递归优化) */ [class]{QuickSortTailCall}-[func]{quickSort}
void quickSort(int[] nums, int left, int right)
{
// 子数组长度为 1 时终止
while (left < right)
{
// 哨兵划分操作
int pivot = partition(nums, left, right);
// 对两个子数组中较短的那个执行快排
if (pivot - left < right - pivot)
{
quickSort(nums, left, pivot - 1); // 递归排序左子数组
left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right]
}
else
{
quickSort(nums, pivot + 1, right); // 递归排序右子数组
right = pivot - 1; // 剩余待排序区间为 [left, pivot - 1]
}
}
}
``` ```
=== "Swift" === "Swift"

View file

@ -607,7 +607,9 @@ comments: true
=== "C#" === "C#"
```csharp title="linkedlist_deque.cs" ```csharp title="linkedlist_deque.cs"
[class]{ListNode}-[func]{}
[class]{LinkedListDeque}-[func]{}
``` ```
=== "Swift" === "Swift"

View file

@ -371,62 +371,7 @@ comments: true
=== "C#" === "C#"
```csharp title="linkedlist_queue.cs" ```csharp title="linkedlist_queue.cs"
/* 基于链表实现的队列 */ [class]{LinkedListQueue}-[func]{}
class LinkedListQueue
{
private ListNode? front, rear; // 头结点 front ,尾结点 rear
private int queSize = 0;
public LinkedListQueue()
{
front = null;
rear = null;
}
/* 获取队列的长度 */
public int size()
{
return queSize;
}
/* 判断队列是否为空 */
public bool isEmpty()
{
return size() == 0;
}
/* 入队 */
public void push(int num)
{
// 尾结点后添加 num
ListNode node = new ListNode(num);
// 如果队列为空,则令头、尾结点都指向该结点
if (front == null)
{
front = node;
rear = node;
// 如果队列不为空,则将该结点添加到尾结点后
}
else if (rear != null)
{
rear.next = node;
rear = node;
}
queSize++;
}
/* 出队 */
public int poll()
{
int num = peek();
// 删除头结点
front = front?.next;
queSize--;
return num;
}
/* 访问队首元素 */
public int peek()
{
if (size() == 0 || front == null)
throw new Exception();
return front.val;
}
}
``` ```
=== "Swift" === "Swift"
@ -570,71 +515,7 @@ comments: true
=== "C#" === "C#"
```csharp title="array_queue.cs" ```csharp title="array_queue.cs"
/* 基于环形数组实现的队列 */ [class]{ArrayQueue}-[func]{}
class ArrayQueue
{
private int[] nums; // 用于存储队列元素的数组
private int front; // 队首指针,指向队首元素
private int queSize; // 队列长度
public ArrayQueue(int capacity)
{
nums = new int[capacity];
front = queSize = 0;
}
/* 获取队列的容量 */
public int capacity()
{
return nums.Length;
}
/* 获取队列的长度 */
public int size()
{
return queSize;
}
/* 判断队列是否为空 */
public bool isEmpty()
{
return queSize == 0;
}
/* 入队 */
public void push(int num)
{
if (queSize == capacity())
{
Console.WriteLine("队列已满");
return;
}
// 计算尾指针,指向队尾索引 + 1
// 通过取余操作,实现 rear 越过数组尾部后回到头部
int rear = (front + queSize) % capacity();
// 尾结点后添加 num
nums[rear] = num;
queSize++;
}
/* 出队 */
public int poll()
{
int num = peek();
// 队首指针向后移动一位,若越过尾部则返回到数组头部
front = (front + 1) % capacity();
queSize--;
return num;
}
/* 访问队首元素 */
public int peek()
{
if (isEmpty())
throw new Exception();
return nums[front];
}
}
``` ```
=== "Swift" === "Swift"

View file

@ -374,49 +374,7 @@ comments: true
=== "C#" === "C#"
```csharp title="linkedlist_stack.cs" ```csharp title="linkedlist_stack.cs"
/* 基于链表实现的栈 */ [class]{LinkedListStack}-[func]{}
class LinkedListStack
{
private ListNode stackPeek; // 将头结点作为栈顶
private int stkSize = 0; // 栈的长度
public LinkedListStack()
{
stackPeek = null;
}
/* 获取栈的长度 */
public int size()
{
return stkSize;
}
/* 判断栈是否为空 */
public bool isEmpty()
{
return size() == 0;
}
/* 入栈 */
public void push(int num)
{
ListNode node = new ListNode(num);
node.next = stackPeek;
stackPeek = node;
stkSize++;
}
/* 出栈 */
public int pop()
{
int num = peek();
stackPeek = stackPeek?.next;
stkSize--;
return num;
}
/* 访问栈顶元素 */
public int peek()
{
if (size() == 0)
throw new Exception();
return stackPeek.val;
}
}
``` ```
=== "Swift" === "Swift"
@ -537,47 +495,7 @@ comments: true
=== "C#" === "C#"
```csharp title="array_stack.cs" ```csharp title="array_stack.cs"
/* 基于数组实现的栈 */ [class]{ArrayStack}-[func]{}
class ArrayStack
{
private List<int> stack;
public ArrayStack()
{
// 初始化列表(动态数组)
stack = new();
}
/* 获取栈的长度 */
public int size()
{
return stack.Count();
}
/* 判断栈是否为空 */
public bool isEmpty()
{
return size() == 0;
}
/* 入栈 */
public void push(int num)
{
stack.Add(num);
}
/* 出栈 */
public int pop()
{
if (isEmpty())
throw new Exception();
var val = peek();
stack.RemoveAt(size() - 1);
return val;
}
/* 访问栈顶元素 */
public int peek()
{
if (isEmpty())
throw new Exception();
return stack[size() - 1];
}
}
``` ```
=== "Swift" === "Swift"

View file

@ -253,19 +253,9 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "C#" === "C#"
```csharp title="avl_tree.cs" ```csharp title="avl_tree.cs"
/* 获取结点高度 */ [class]{AVLTree}-[func]{height}
public int height(TreeNode? node)
{
// 空结点高度为 -1 ,叶结点高度为 0
return node == null ? -1 : node.height;
}
/* 更新结点高度 */ [class]{AVLTree}-[func]{updateHeight}
private void updateHeight(TreeNode node)
{
// 结点高度等于最高子树高度 + 1
node.height = Math.Max(height(node.left), height(node.right)) + 1;
}
``` ```
=== "Swift" === "Swift"
@ -349,14 +339,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "C#" === "C#"
```csharp title="avl_tree.cs" ```csharp title="avl_tree.cs"
/* 获取平衡因子 */ [class]{AVLTree}-[func]{balanceFactor}
public int balanceFactor(TreeNode? node)
{
// 空结点平衡因子为 0
if (node == null) return 0;
// 结点平衡因子 = 左子树高度 - 右子树高度
return height(node.left) - height(node.right);
}
``` ```
=== "Swift" === "Swift"
@ -460,20 +443,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "C#" === "C#"
```csharp title="avl_tree.cs" ```csharp title="avl_tree.cs"
/* 右旋操作 */ [class]{AVLTree}-[func]{rightRotate}
TreeNode? rightRotate(TreeNode? node)
{
TreeNode? child = node.left;
TreeNode? grandChild = child?.right;
// 以 child 为原点,将 node 向右旋转
child.right = node;
node.left = grandChild;
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根结点
return child;
}
``` ```
=== "Swift" === "Swift"
@ -559,20 +529,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "C#" === "C#"
```csharp title="avl_tree.cs" ```csharp title="avl_tree.cs"
/* 左旋操作 */ [class]{AVLTree}-[func]{leftRotate}
TreeNode? leftRotate(TreeNode? node)
{
TreeNode? child = node.right;
TreeNode? grandChild = child?.left;
// 以 child 为原点,将 node 向左旋转
child.left = node;
node.right = grandChild;
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根结点
return child;
}
``` ```
=== "Swift" === "Swift"
@ -694,44 +651,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "C#" === "C#"
```csharp title="avl_tree.cs" ```csharp title="avl_tree.cs"
/* 执行旋转操作,使该子树重新恢复平衡 */ [class]{AVLTree}-[func]{rotate}
TreeNode? rotate(TreeNode? node)
{
// 获取结点 node 的平衡因子
int balanceFactorInt = balanceFactor(node);
// 左偏树
if (balanceFactorInt > 1)
{
if (balanceFactor(node.left) >= 0)
{
// 右旋
return rightRotate(node);
}
else
{
// 先左旋后右旋
node.left = leftRotate(node?.left);
return rightRotate(node);
}
}
// 右偏树
if (balanceFactorInt < -1)
{
if (balanceFactor(node.right) <= 0)
{
// 左旋
return leftRotate(node);
}
else
{
// 先右旋后左旋
node.right = rightRotate(node?.right);
return leftRotate(node);
}
}
// 平衡树,无需旋转,直接返回
return node;
}
``` ```
=== "Swift" === "Swift"
@ -889,30 +809,9 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "C#" === "C#"
```csharp title="avl_tree.cs" ```csharp title="avl_tree.cs"
/* 插入结点 */ [class]{AVLTree}-[func]{insert}
public TreeNode? insert(int val)
{
root = insertHelper(root, val);
return root;
}
/* 递归插入结点(辅助函数) */ [class]{AVLTree}-[func]{insertHelper}
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);
else if (val > node.val)
node.right = insertHelper(node.right, val);
else
return node; // 重复结点不插入,直接返回
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根结点
return node;
}
``` ```
=== "Swift" === "Swift"
@ -1186,48 +1085,11 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "C#" === "C#"
```csharp title="avl_tree.cs" ```csharp title="avl_tree.cs"
/* 删除结点 */ [class]{AVLTree}-[func]{remove}
public TreeNode? remove(int val)
{
root = removeHelper(root, val);
return root;
}
/* 递归删除结点(辅助函数) */ [class]{AVLTree}-[func]{removeHelper}
private TreeNode? removeHelper(TreeNode? node, int val)
{ [class]{AVLTree}-[func]{getInOrderNext}
if (node == null) return null;
/* 1. 查找结点,并删除之 */
if (val < node.val)
node.left = removeHelper(node.left, val);
else if (val > node.val)
node.right = removeHelper(node.right, val);
else
{
if (node.left == null || node.right == null)
{
TreeNode? child = node.left != null ? node.left : node.right;
// 子结点数量 = 0 ,直接删除 node 并返回
if (child == null)
return null;
// 子结点数量 = 1 ,直接删除 node
else
node = child;
}
else
{
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
TreeNode? temp = getInOrderNext(node.right);
node.right = removeHelper(node.right, temp.val);
node.val = temp.val;
}
}
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根结点
return node;
}
``` ```
=== "Swift" === "Swift"

View file

@ -98,23 +98,7 @@ comments: true
=== "C#" === "C#"
```csharp title="binary_search_tree.cs" ```csharp title="binary_search_tree.cs"
/* 查找结点 */ [class]{BinarySearchTree}-[func]{search}
TreeNode? search(int num)
{
TreeNode? cur = root;
// 循环查找,越过叶结点后跳出
while (cur != null)
{
// 目标结点在 cur 的右子树中
if (cur.val < num) cur = cur.right;
// 目标结点在 cur 的左子树中
else if (cur.val > num) cur = cur.left;
// 找到目标结点,跳出循环
else break;
}
// 返回目标结点
return cur;
}
``` ```
=== "Swift" === "Swift"
@ -214,33 +198,7 @@ comments: true
=== "C#" === "C#"
```csharp title="binary_search_tree.cs" ```csharp title="binary_search_tree.cs"
/* 插入结点 */ [class]{BinarySearchTree}-[func]{insert}
TreeNode? insert(int num)
{
// 若树为空,直接提前返回
if (root == null) return null;
TreeNode? cur = root, pre = null;
// 循环查找,越过叶结点后跳出
while (cur != null)
{
// 找到重复结点,直接返回
if (cur.val == num) return null;
pre = cur;
// 插入位置在 cur 的右子树中
if (cur.val < num) cur = cur.right;
// 插入位置在 cur 的左子树中
else cur = cur.left;
}
// 插入结点 val
TreeNode node = new TreeNode(num);
if (pre != null)
{
if (pre.val < num) pre.right = node;
else pre.left = node;
}
return node;
}
``` ```
=== "Swift" === "Swift"
@ -411,68 +369,9 @@ comments: true
=== "C#" === "C#"
```csharp title="binary_search_tree.cs" ```csharp title="binary_search_tree.cs"
/* 删除结点 */ [class]{BinarySearchTree}-[func]{remove}
TreeNode? remove(int num)
{
// 若树为空,直接提前返回
if (root == null) return null;
TreeNode? cur = root, pre = null;
// 循环查找,越过叶结点后跳出
while (cur != null)
{
// 找到待删除结点,跳出循环
if (cur.val == num) break;
pre = cur;
// 待删除结点在 cur 的右子树中
if (cur.val < num) cur = cur.right;
// 待删除结点在 cur 的左子树中
else cur = cur.left;
}
// 若无待删除结点,则直接返回
if (cur == null || pre == null) return null;
// 子结点数量 = 0 or 1
if (cur.left == null || cur.right == null)
{
// 当子结点数量 = 0 / 1 时, child = null / 该子结点
TreeNode? child = cur.left != null ? cur.left : cur.right;
// 删除结点 cur
if (pre.left == cur)
{
pre.left = child;
}
else
{
pre.right = child;
}
}
// 子结点数量 = 2
else
{
// 获取中序遍历中 cur 的下一个结点
TreeNode? nex = getInOrderNext(cur.right);
if (nex != null)
{
int tmp = nex.val;
// 递归删除结点 nex
remove(nex.val);
// 将 nex 的值复制给 cur
cur.val = tmp;
}
}
return cur;
}
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */ [class]{BinarySearchTree}-[func]{getInOrderNext}
private TreeNode? getInOrderNext(TreeNode? root)
{
if (root == null) return root;
// 循环访问左子结点,直到叶结点时为最小结点,跳出
while (root.left != null)
{
root = root.left;
}
return root;
}
``` ```
=== "Swift" === "Swift"

View file

@ -85,26 +85,7 @@ comments: true
=== "C#" === "C#"
```csharp title="binary_tree_bfs.cs" ```csharp title="binary_tree_bfs.cs"
/* 层序遍历 */ [class]{binary_tree_bfs}-[func]{hierOrder}
public List<int?> hierOrder(TreeNode root)
{
// 初始化队列,加入根结点
Queue<TreeNode> queue = new();
queue.Enqueue(root);
// 初始化一个列表,用于保存遍历序列
List<int> list = new();
while (queue.Count != 0)
{
TreeNode node = queue.Dequeue(); // 队列出队
list.Add(node.val); // 保存结点值
if (node.left != null)
queue.Enqueue(node.left); // 左子结点入队
if (node.right != null)
queue.Enqueue(node.right); // 右子结点入队
}
return list;
}
``` ```
=== "Swift" === "Swift"
@ -235,35 +216,11 @@ comments: true
=== "C#" === "C#"
```csharp title="binary_tree_dfs.cs" ```csharp title="binary_tree_dfs.cs"
/* 前序遍历 */ [class]{binary_tree_dfs}-[func]{preOrder}
void preOrder(TreeNode? root)
{
if (root == null) return;
// 访问优先级:根结点 -> 左子树 -> 右子树
list.Add(root.val);
preOrder(root.left);
preOrder(root.right);
}
/* 中序遍历 */ [class]{binary_tree_dfs}-[func]{inOrder}
void inOrder(TreeNode? root)
{
if (root == null) return;
// 访问优先级:左子树 -> 根结点 -> 右子树
inOrder(root.left);
list.Add(root.val);
inOrder(root.right);
}
/* 后序遍历 */ [class]{binary_tree_dfs}-[func]{postOrder}
void postOrder(TreeNode? root)
{
if (root == null) return;
// 访问优先级:左子树 -> 右子树 -> 根结点
postOrder(root.left);
postOrder(root.right);
list.Add(root.val);
}
``` ```
=== "Swift" === "Swift"

View file

@ -15,6 +15,7 @@ from docs.utils.extract_code_java import ExtractCodeBlocksJava
from docs.utils.extract_code_cpp import ExtractCodeBlocksCpp from docs.utils.extract_code_cpp import ExtractCodeBlocksCpp
from docs.utils.extract_code_jsts import ExtractCodeBlocksJSTS from docs.utils.extract_code_jsts import ExtractCodeBlocksJSTS
from docs.utils.extract_code_swift import ExtractCodeBlocksSwift from docs.utils.extract_code_swift import ExtractCodeBlocksSwift
from docs.utils.extract_code_csharp import ExtractCodeBlocksCSharp
def build_markdown(md_path): def build_markdown(md_path):
@ -42,8 +43,12 @@ def build_markdown(md_path):
extractor = extractor_dict[lang] extractor = extractor_dict[lang]
# Get code blocks # Get code blocks
if file_name not in code_blocks_dict: if file_name not in code_blocks_dict:
code_blocks_dict[file_name] = extractor.extract( code_blocks = extractor.extract(
file_path=osp.dirname(md_path).replace("docs/", f"codes/{lang}/") + f"/{file_name}") file_path=osp.dirname(md_path).replace("docs/", f"codes/{lang}/") + f"/{file_name}")
if code_blocks is None:
i += 1
continue
code_blocks_dict[file_name] = code_blocks
header_line = i header_line = i
class_label = src_match[1] class_label = src_match[1]
@ -90,6 +95,7 @@ extractor_dict = {
"javascript": ExtractCodeBlocksJSTS(), "javascript": ExtractCodeBlocksJSTS(),
"typescript": ExtractCodeBlocksJSTS(), "typescript": ExtractCodeBlocksJSTS(),
"swift": ExtractCodeBlocksSwift(), "swift": ExtractCodeBlocksSwift(),
"csharp": ExtractCodeBlocksCSharp(),
} }

View file

@ -1,14 +1,27 @@
# This script is borrowed from https://gist.github.com/cobyism/4730490 python docs/utils/build_markdown.py
while true; do
read -p "Do you wish to deploy the site? [y] [n]" yn
case $yn in
[Yy]* ) make install; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes[y] or no[n].";;
esac
done
# push the built docs
cd build cd build
git add . git add .
git commit -m "build" git commit -m "build"
git push -u origin docs git push -u origin docs
cd .. cd ..
# Build mkdocs
mkdocs build --clean mkdocs build --clean
# deploy the site
cd site cd site
git add . git add .
git commit -m "deploy" git commit -m "deploy"
git push -u origin gh-pages git push -u origin gh-pages
cd..

View file

@ -0,0 +1,28 @@
"""
File: extract_code_csharp.py
Created Time: 2023-02-07
Author: Krahets (krahets@163.com)
"""
import re
import glob
import sys, os.path as osp
sys.path.append(osp.dirname(osp.dirname(osp.dirname(osp.abspath(__file__)))))
from docs.utils.extract_code_java import ExtractCodeBlocksJava
class ExtractCodeBlocksCSharp(ExtractCodeBlocksJava):
def __init__(self) -> None:
super().__init__()
# Pattern to match function names and class names
self.func_pattern = r'(\s*)(public|private|)\s*(static|)\s*(|\S+)\s*(\w+)(\(.*\))'
self.class_pattern = r'(public|)\s*(class|struct)\s+(\w+)\s*\n'
self.func_pattern_keys = ["total", "ind", "scope", "static", "return", "label", "args"]
self.class_pattern_keys = ["total", "scope", "type", "label"]
# for code_path in glob.glob("codes/csharp/chapter_*/array.cs"):
# ext = ExtractCodeBlocksCSharp()
# res = ext.extract(code_path)
# pass

View file

@ -31,6 +31,9 @@ class ExtractCodeBlocksJava:
""" """
Extract classes and functions from a markdown document Extract classes and functions from a markdown document
""" """
if not osp.isfile(file_path):
return None
self.file_path = file_path self.file_path = file_path
with open(file_path) as f: with open(file_path) as f:
self.lines = f.readlines() self.lines = f.readlines()

View file

@ -21,8 +21,3 @@ class ExtractCodeBlocksJSTS(ExtractCodeBlocksJava):
self.func_pattern_keys = ["total", "ind", "prefix", "label", ":", "return"] self.func_pattern_keys = ["total", "ind", "prefix", "label", ":", "return"]
self.class_pattern_keys = ["total", "scope", "label"] self.class_pattern_keys = ["total", "scope", "label"]
# for code_path in glob.glob("codes/cpp/chapter_*/my_heap.cpp"):
# ext = ExtractCodeBlocksCpp()
# ext.extract(code_path)

View file

@ -49,7 +49,3 @@ class ExtractCodeBlocksPython(ExtractCodeBlocksJava):
remove_empty_lines(func) remove_empty_lines(func)
for func in funcs.values(): for func in funcs.values():
remove_empty_lines(func) remove_empty_lines(func)
# ext = ExtractCodeBlocksPython()
# ext.extract("codes/python/chapter_array_and_linkedlist/my_list.py")

View file

@ -21,8 +21,3 @@ class ExtractCodeBlocksSwift(ExtractCodeBlocksJava):
self.func_pattern_keys = ["total", "ind", "scope", "static", "func", "label"] self.func_pattern_keys = ["total", "ind", "scope", "static", "func", "label"]
self.class_pattern_keys = ["total", "scope", "label"] self.class_pattern_keys = ["total", "scope", "label"]
# for code_path in glob.glob("codes/cpp/chapter_*/my_heap.cpp"):
# ext = ExtractCodeBlocksCpp()
# ext.extract(code_path)