Update C# code.

This commit is contained in:
Yudong Jin
2022-12-24 17:05:58 +08:00
parent 4906021ccb
commit 8733557f00
16 changed files with 88 additions and 118 deletions

View File

@@ -25,7 +25,7 @@ namespace hello_algo.chapter_array_and_linkedlist
Console.WriteLine("访问索引 1 处的元素,得到 num = " + num);
/* 更新元素 */
list[1]=0;
list[1] = 0;
Console.WriteLine("将索引 1 处的元素更新为 0 ,得到 list = " + string.Join(",", list));
/* 清空列表 */

View File

@@ -14,7 +14,7 @@ namespace hello_algo.chapter_computational_complexity
{
int a = 1; // +0技巧 1
a = a + n; // +0技巧 1
// +n技巧 2
// +n技巧 2
for (int i = 0; i < 5 * n + 1; i++)
{
Console.WriteLine(0);
@@ -101,7 +101,7 @@ namespace hello_algo.chapter_computational_complexity
static int bubbleSort(int[] nums)
{
int count = 0; // 计数器
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
for (int i = nums.Length - 1; i > 0; i--)
{
// 内循环:冒泡操作

View File

@@ -10,36 +10,43 @@ namespace hello_algo.chapter_stack_and_queue
{
/* 基于环形数组实现的队列 */
class ArrayQueue {
class ArrayQueue
{
private int[] nums; // 用于存储队列元素的数组
private int front = 0; // 头指针,指向队首
private int rear = 0; // 尾指针,指向队尾 + 1
public ArrayQueue(int capacity) {
public ArrayQueue(int capacity)
{
// 初始化数组
nums = new int[capacity];
}
/* 获取队列的容量 */
public int capacity() {
public int capacity()
{
return nums.Length;
}
/* 获取队列的长度 */
public int size() {
public int size()
{
int capacity = this.capacity();
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
return (capacity + rear - front) % capacity;
}
/* 判断队列是否为空 */
public bool isEmpty() {
public bool isEmpty()
{
return rear - front == 0;
}
/* 入队 */
public void offer(int num) {
if (size() == capacity()) {
public void offer(int num)
{
if (size() == capacity())
{
Console.WriteLine("队列已满");
return;
}
@@ -50,7 +57,8 @@ namespace hello_algo.chapter_stack_and_queue
}
/* 出队 */
public int poll() {
public int poll()
{
int num = peek();
// 队头指针向后移动一位,若越过尾部则返回到数组头部
front = (front + 1) % capacity();
@@ -58,26 +66,30 @@ namespace hello_algo.chapter_stack_and_queue
}
/* 访问队首元素 */
public int peek() {
public int peek()
{
if (isEmpty())
throw new Exception();
return nums[front];
}
/* 返回数组 */
public int[] toArray() {
public int[] toArray()
{
int size = this.size();
int capacity = this.capacity();
// 仅转换有效长度范围内的列表元素
int[] res = new int[size];
for (int i = 0, j = front; i < size; i++, j++) {
for (int i = 0, j = front; i < size; i++, j++)
{
res[i] = nums[j % capacity];
}
return res;
}
}
public class array_queue {
public class array_queue
{
[Test]
public void Test()
{
@@ -91,7 +103,7 @@ namespace hello_algo.chapter_stack_and_queue
queue.offer(2);
queue.offer(5);
queue.offer(4);
Console.WriteLine("队列 queue = " + string.Join(",",queue.toArray()));
Console.WriteLine("队列 queue = " + string.Join(",", queue.toArray()));
/* 访问队首元素 */
int peek = queue.peek();
@@ -110,7 +122,8 @@ namespace hello_algo.chapter_stack_and_queue
Console.WriteLine("队列是否为空 = " + isEmpty);
/* 测试环形数组 */
for (int i = 0; i < 10; i++) {
for (int i = 0; i < 10; i++)
{
queue.offer(i);
queue.poll();
Console.WriteLine("第 " + i + " 轮入队 + 出队后 queue = " + string.Join(",", queue.toArray()));

View File

@@ -40,9 +40,6 @@ namespace hello_algo.chapter_tree
/* 右旋操作 */
TreeNode? rightRotate(TreeNode? node)
{
if (node == null)
return null;
TreeNode? child = node.left;
TreeNode? grandChild = child?.right;
// 以 child 为原点,将 node 向右旋转
@@ -58,9 +55,6 @@ namespace hello_algo.chapter_tree
/* 左旋操作 */
TreeNode? leftRotate(TreeNode? node)
{
if (node == null)
return null;
TreeNode? child = node.right;
TreeNode? grandChild = child?.left;
// 以 child 为原点,将 node 向左旋转
@@ -76,9 +70,6 @@ namespace hello_algo.chapter_tree
/* 执行旋转操作,使该子树重新恢复平衡 */
TreeNode? rotate(TreeNode? node)
{
if (node == null)
return node;
// 获取结点 node 的平衡因子
int balanceFactorInt = balanceFactor(node);
// 左偏树