Skip to content

Commit

Permalink
Update C# code.
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Dec 24, 2022
1 parent 4906021 commit 8733557
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 118 deletions.
2 changes: 1 addition & 1 deletion codes/csharp/chapter_array_and_linkedlist/list.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void Test()
Console.WriteLine("访问索引 1 处的元素,得到 num = " + num);

/* 更新元素 */
list[1]=0;
list[1] = 0;
Console.WriteLine("将索引 1 处的元素更新为 0 ,得到 list = " + string.Join(",", list));

/* 清空列表 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void algorithm(int n)
{
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);
Expand Down Expand Up @@ -101,7 +101,7 @@ static int quadratic(int n)
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--)
{
// 内循环:冒泡操作
Expand Down
41 changes: 27 additions & 14 deletions codes/csharp/chapter_stack_and_queue/array_queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -50,34 +57,39 @@ public void offer(int num) {
}

/* 出队 */
public int poll() {
public int poll()
{
int num = peek();
// 队头指针向后移动一位,若越过尾部则返回到数组头部
front = (front + 1) % capacity();
return num;
}

/* 访问队首元素 */
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()
{
Expand All @@ -91,7 +103,7 @@ public void Test()
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();
Expand All @@ -110,7 +122,8 @@ public void Test()
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()));
Expand Down
9 changes: 0 additions & 9 deletions codes/csharp/chapter_tree/avl_tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ public int balanceFactor(TreeNode? node)
/* 右旋操作 */
TreeNode? rightRotate(TreeNode? node)
{
if (node == null)
return null;

TreeNode? child = node.left;
TreeNode? grandChild = child?.right;
// 以 child 为原点,将 node 向右旋转
Expand All @@ -58,9 +55,6 @@ public int balanceFactor(TreeNode? node)
/* 左旋操作 */
TreeNode? leftRotate(TreeNode? node)
{
if (node == null)
return null;

TreeNode? child = node.right;
TreeNode? grandChild = child?.left;
// 以 child 为原点,将 node 向左旋转
Expand All @@ -76,9 +70,6 @@ public int balanceFactor(TreeNode? node)
/* 执行旋转操作,使该子树重新恢复平衡 */
TreeNode? rotate(TreeNode? node)
{
if (node == null)
return node;

// 获取结点 node 的平衡因子
int balanceFactorInt = balanceFactor(node);
// 左偏树
Expand Down
4 changes: 2 additions & 2 deletions docs/chapter_array_and_linkedlist/linked_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ comments: true
=== "C#"

```csharp title=""
// 链表结点类
/* 链表结点类 */
class ListNode
{
int val; // 结点值
Expand Down Expand Up @@ -675,7 +675,7 @@ comments: true
=== "C#"

```csharp title=""
// 双向链表结点类
/* 双向链表结点类 */
class ListNode {
int val; // 结点值
ListNode next; // 指向后继结点的指针(引用)
Expand Down
20 changes: 10 additions & 10 deletions docs/chapter_array_and_linkedlist/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,20 @@ comments: true

```js title="list.js"
/* 访问元素 */
const num = list[1];
const num = list[1]; // 访问索引 1 处的元素

/* 更新元素 */
list[1] = 0;
list[1] = 0; // 将索引 1 处的元素更新为 0
```

=== "TypeScript"

```typescript title="list.ts"
/* 访问元素 */
const num: number = list[1];
const num: number = list[1]; // 访问索引 1 处的元素

/* 更新元素 */
list[1] = 0;
list[1] = 0; // 将索引 1 处的元素更新为 0
```

=== "C"
Expand All @@ -163,10 +163,10 @@ comments: true

```csharp title="list.cs"
/* 访问元素 */
int num = list[1];
int num = list[1]; // 访问索引 1 处的元素

/* 更新元素 */
list[1]=0;
list[1] = 0; // 将索引 1 处的元素更新为 0
```

**在列表中添加、插入、删除元素。** 相对于数组,列表可以自由地添加与删除元素。在列表尾部添加元素的时间复杂度为 $O(1)$ ,但是插入与删除元素的效率仍与数组一样低,时间复杂度为 $O(N)$ 。
Expand Down Expand Up @@ -477,15 +477,15 @@ comments: true
```js title="list.js"
/* 拼接两个列表 */
const list1 = [6, 8, 7, 10, 9];
list.push(...list1);
list.push(...list1); // 将列表 list1 拼接到 list 之后
```

=== "TypeScript"

```typescript title="list.ts"
/* 拼接两个列表 */
const list1: number[] = [6, 8, 7, 10, 9];
list.push(...list1);
list.push(...list1); // 将列表 list1 拼接到 list 之后
```

=== "C"
Expand All @@ -499,7 +499,7 @@ comments: true
```csharp title="list.cs"
/* 拼接两个列表 */
List<int> list1 = new() { 6, 8, 7, 10, 9 };
list.AddRange(list1);
list.AddRange(list1); // 将列表 list1 拼接到 list 之后
```

**排序列表。** 排序也是常用的方法之一,完成列表排序后,我们就可以使用在数组类算法题中经常考察的「二分查找」和「双指针」算法了。
Expand Down Expand Up @@ -536,7 +536,7 @@ comments: true

```js title="list.js"
/* 排序列表 */
list.sort((a, b) => a - b);
list.sort((a, b) => a - b); // 排序后,列表元素从小到大排列
```

=== "TypeScript"
Expand Down
12 changes: 6 additions & 6 deletions docs/chapter_computational_complexity/space_complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ comments: true
return 0;
}

int algorithm(int n)
{ // 输入数据
int a = 0; // 暂存数据(常量)
int algorithm(int n) // 输入数据
{
int a = 0; // 暂存数据(常量)
int b = 0; // 暂存数据(变量)
Node node = new Node(0); // 暂存数据(对象)
int c = function(); // 栈帧空间(调用函数)
Expand Down Expand Up @@ -606,7 +606,7 @@ $$
nodes = append(nodes, newNode(i))
}
// 长度为 n 的哈希表占用 O(n) 空间
m := make(map[int]string, n)
m := make(map[int]string, n)
for i := 0; i < n; i++ {
m[i] = strconv.Itoa(i)
}
Expand Down Expand Up @@ -883,8 +883,8 @@ $$
if n <= 0 {
return 0
}
// 数组 nums 长度为 n, n-1, ..., 2, 1
nums := make([]int, n)
fmt.Printf("递归 n = %d 中的 nums 长度 = %d \n", n, len(nums))
return spaceQuadraticRecur(n - 1)
}
```
Expand Down Expand Up @@ -914,8 +914,8 @@ $$
int quadraticRecur(int n)
{
if (n <= 0) return 0;
// 数组 nums 长度为 n, n-1, ..., 2, 1
int[] nums = new int[n];
Console.WriteLine("递归 n = " + n + " 中的 nums 长度 = " + nums.Length);
return quadraticRecur(n - 1);
}

Expand Down
8 changes: 4 additions & 4 deletions docs/chapter_computational_complexity/time_complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ $$
int a = 2; // 1 ns
a = a + 1; // 1 ns
a = a * 2; // 10 ns
// 循环 n 次
// 循环 n 次
for (int i = 0; i < n; i++)
{ // 1 ns ,每轮都要执行 i++
Console.WriteLine(0); // 5 ns
Expand Down Expand Up @@ -347,7 +347,7 @@ $$
a = a * 2; // +1
// 循环 n 次
for (int i = 0; i < n; i++) { // +1(每轮都执行 i ++)
Console.WriteLine(0); // +1
Console.WriteLine(0); // +1
}
}
```
Expand Down Expand Up @@ -500,7 +500,7 @@ $$
{
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);
Expand Down Expand Up @@ -1422,7 +1422,7 @@ $$
=== "C#"

```csharp title="time_complexity.cs"
/* 对数阶(递归实现) */
/* 对数阶(递归实现) */
int logRecur(float n)
{
if (n <= 1) return 0;
Expand Down
5 changes: 0 additions & 5 deletions docs/chapter_hashing/hash_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,14 +543,12 @@ $$
bucket.Add(null);
}
}

/* 哈希函数 */
private int hashFunc(int key)
{
int index = key % 100;
return index;
}

/* 查询操作 */
public String? get(int key)
{
Expand All @@ -559,15 +557,13 @@ $$
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)
{
Expand All @@ -576,7 +572,6 @@ $$
bucket[index]=null;
}
}

```

## 哈希冲突
Expand Down
2 changes: 1 addition & 1 deletion docs/chapter_searching/binary_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ $$
=== "C#"

```csharp title=""
// (i + j) 有可能超出 int 的取值范围
// (i + j) 有可能超出 int 的取值范围
int m = (i + j) / 2;
// 更换为此写法则不会越界
int m = i + (j - i) / 2;
Expand Down
Loading

0 comments on commit 8733557

Please sign in to comment.