Skip to content

Commit c4d3262

Browse files
authored
Merge branch 'master' into master
2 parents 8b401c2 + 36507b8 commit c4d3262

File tree

139 files changed

+6755
-907
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+6755
-907
lines changed

.github/pull_request_template.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
> Tip: If this PR is not related to the coding or code translation, please ignore the checklist.
1+
If this PR is related to coding or code translation, please fill out the checklist.
22

3-
### Checklist
4-
5-
- [] I've tested the code and ensured the outputs are the same as the outputs of reference codes.
6-
- [] I've checked the codes (formatting, comments, indentation, file header, etc) carefully.
7-
- [] The code is not relied on a particular environment or IDE and can be runned on a common system (Win, MacOS, Ubuntu).
3+
- [ ] I've tested the code and ensured the outputs are the same as the outputs of reference codes.
4+
- [ ] I've checked the codes (formatting, comments, indentation, file header, etc) carefully.
5+
- [ ] The code does not rely on a particular environment or IDE and can be executed on a standard system (Win, macOS, Ubuntu).

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ docs/overrides/
1313

1414
# python files
1515
__pycache__
16+
17+
# iml
18+
hello-algo.iml

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
- 开源免费,所有同学都可在网上获取本书;
3737
- 新手友好,适合算法初学者自主学习入门;
3838
- 动画讲解,尽可能地保证平滑的学习曲线;
39-
- 代码导向,提供精简、可运行的算法代码
39+
- 代码导向,提供可一键运行的算法源代码
4040
- 讨论学习,提问一般能在三日内得到回复;
4141

4242
如果感觉本书对你有所帮助,请点个 Star :star: 支持一下,谢谢!

codes/cpp/chapter_array_and_linkedlist/array.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ int* extend(int* nums, int size, int enlarge) {
3030
/* 在数组的索引 index 处插入元素 num */
3131
void insert(int* nums, int size, int num, int index) {
3232
// 把索引 index 以及之后的所有元素向后移动一位
33-
for (int i = size - 1; i >= index; i--) {
33+
for (int i = size - 1; i > index; i--) {
3434
nums[i] = nums[i - 1];
3535
}
3636
// 将 num 赋给 index 处元素

codes/cpp/chapter_sorting/merge_sort.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ void merge(vector<int>& nums, int left, int mid, int right) {
2222
int i = leftStart, j = rightStart;
2323
// 通过覆盖原数组 nums 来合并左子数组和右子数组
2424
for (int k = left; k <= right; k++) {
25-
// “左子数组已全部合并完”,则选取右子数组元素,并且 j++
25+
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
2626
if (i > leftEnd)
2727
nums[k] = tmp[j++];
28-
// 否则,若 “右子数组已全部合并完”“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
28+
// 否则,若“右子数组已全部合并完”“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
2929
else if (j > rightEnd || tmp[i] <= tmp[j])
3030
nums[k] = tmp[i++];
31-
// 否则,若 “左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
31+
// 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
3232
else
3333
nums[k] = tmp[j++];
3434
}

codes/cpp/chapter_stack_and_queue/array_queue.cpp

+1-13
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,11 @@ class ArrayQueue {
5959

6060
/* 访问队首元素 */
6161
int peek() {
62-
// 删除头结点
6362
if (empty())
6463
throw out_of_range("队列为空");
6564
return nums[front];
6665
}
6766

68-
/* 访问指定索引元素 */
69-
int get(int index) {
70-
if (index >= size())
71-
throw out_of_range("索引越界");
72-
return nums[(front + index) % capacity()];
73-
}
74-
7567
/* 将数组转化为 Vector 并返回 */
7668
vector<int> toVector() {
7769
int siz = size();
@@ -104,11 +96,7 @@ int main() {
10496
/* 访问队首元素 */
10597
int peek = queue->peek();
10698
cout << "队首元素 peek = " << peek << endl;
107-
108-
/* 访问指定索引元素 */
109-
int num = queue->get(2);
110-
cout << "队列第 3 个元素为 num = " << num << endl;
111-
99+
112100
/* 元素出队 */
113101
int poll = queue->poll();
114102
cout << "出队元素 poll = " << poll << ",出队后 queue = ";

codes/cpp/chapter_stack_and_queue/array_stack.cpp

+3-10
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,18 @@ class ArrayStack {
2929

3030
/* 出栈 */
3131
int pop() {
32-
int oldTop = stack.back();
32+
int oldTop = top();
3333
stack.pop_back();
3434
return oldTop;
3535
}
3636

3737
/* 访问栈顶元素 */
3838
int top() {
39+
if(empty())
40+
throw out_of_range("栈为空");
3941
return stack.back();
4042
}
4143

42-
/* 访问索引 index 处元素 */
43-
int get(int index) {
44-
return stack[index];
45-
}
46-
4744
/* 返回 Vector */
4845
vector<int> toVector() {
4946
return stack;
@@ -69,10 +66,6 @@ int main() {
6966
int top = stack->top();
7067
cout << "栈顶元素 top = " << top << endl;
7168

72-
/* 访问索引 index 处元素 */
73-
int num = stack->get(3);
74-
cout << "栈索引 3 处的元素为 num = " << num << endl;
75-
7669
/* 元素出栈 */
7770
int pop = stack->pop();
7871
cout << "出栈元素 pop = " << pop << ",出栈后 stack = ";

codes/csharp/chapter_array_and_linkedlist/Array.cs renamed to codes/csharp/chapter_array_and_linkedlist/array.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// File: Array.cs
1+
// File: array.cs
22
// Created Time: 2022-12-14
33
// Author: mingXta ([email protected])
44

@@ -41,7 +41,7 @@ public static int[] Extend(int[] nums, int enlarge)
4141
public static void Insert(int[] nums, int num, int index)
4242
{
4343
// 把索引 index 以及之后的所有元素向后移动一位
44-
for (int i = nums.Length - 1; i >= index; i--)
44+
for (int i = nums.Length - 1; i > index; i--)
4545
{
4646
nums[i] = nums[i - 1];
4747
}
@@ -134,4 +134,4 @@ public static void Test()
134134
Console.WriteLine("在 nums 中查找元素 3 ,得到索引 = " + index);
135135
}
136136
}
137-
}
137+
}

codes/csharp/chapter_array_and_linkedlist/LinkedList.cs renamed to codes/csharp/chapter_array_and_linkedlist/linked_list.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// File: LinkedList.cs
1+
// File: linked_list.cs
22
// Created Time: 2022-12-16
33
// Author: mingXta ([email protected])
44

@@ -7,14 +7,14 @@
77

88
namespace hello_algo.chapter_array_and_linkedlist
99
{
10-
public class LinkedList
10+
public class linked_list
1111
{
1212
/// <summary>
1313
/// 在链表的结点 n0 之后插入结点 P
1414
/// </summary>
1515
public static void Insert(ListNode n0, ListNode P)
1616
{
17-
ListNode n1 = n0.next;
17+
ListNode? n1 = n0.next;
1818
n0.next = P;
1919
P.next = n1;
2020
}
@@ -28,14 +28,14 @@ public static void Remove(ListNode n0)
2828
return;
2929
// n0 -> P -> n1
3030
ListNode P = n0.next;
31-
ListNode n1 = P.next;
31+
ListNode? n1 = P.next;
3232
n0.next = n1;
3333
}
3434

3535
/// <summary>
3636
/// 访问链表中索引为 index 的结点
3737
/// </summary>
38-
public static ListNode Access(ListNode head, int index)
38+
public static ListNode? Access(ListNode head, int index)
3939
{
4040
for (int i = 0; i < index; i++)
4141
{
@@ -89,12 +89,12 @@ public void Test()
8989
Console.WriteLine($"删除结点后的链表为{n0}");
9090

9191
// 访问结点
92-
ListNode node = Access(n0, 3);
93-
Console.WriteLine($"链表中索引 3 处的结点的值 = {node.val}");
92+
ListNode? node = Access(n0, 3);
93+
Console.WriteLine($"链表中索引 3 处的结点的值 = {node?.val}");
9494

9595
// 查找结点
9696
int index = Find(n0, 2);
9797
Console.WriteLine($"链表中值为 2 的结点的索引 = {index}");
9898
}
9999
}
100-
}
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* File: list.cs
3+
* Created Time: 2022-12-23
4+
* Author: haptear ([email protected])
5+
*/
6+
7+
using NUnit.Framework;
8+
9+
namespace hello_algo.chapter_array_and_linkedlist
10+
{
11+
public class list
12+
{
13+
[Test]
14+
public void Test()
15+
{
16+
17+
/* 初始化列表 */
18+
// 注意数组的元素类型是 int[] 的包装类 int[]
19+
int[] numbers = new int[] { 1, 3, 2, 5, 4 };
20+
List<int> list = numbers.ToList();
21+
Console.WriteLine("列表 list = " + string.Join(",",list));
22+
23+
/* 访问元素 */
24+
int num = list[1];
25+
Console.WriteLine("访问索引 1 处的元素,得到 num = " + num);
26+
27+
/* 更新元素 */
28+
list[1] = 0;
29+
Console.WriteLine("将索引 1 处的元素更新为 0 ,得到 list = " + string.Join(",", list));
30+
31+
/* 清空列表 */
32+
list.Clear();
33+
Console.WriteLine("清空列表后 list = " + string.Join(",", list));
34+
35+
/* 尾部添加元素 */
36+
list.Add(1);
37+
list.Add(3);
38+
list.Add(2);
39+
list.Add(5);
40+
list.Add(4);
41+
Console.WriteLine("添加元素后 list = " + string.Join(",", list));
42+
43+
/* 中间插入元素 */
44+
list.Insert(3, 6);
45+
Console.WriteLine("在索引 3 处插入数字 6 ,得到 list = " + string.Join(",", list));
46+
47+
/* 删除元素 */
48+
list.RemoveAt(3);
49+
Console.WriteLine("删除索引 3 处的元素,得到 list = " + string.Join(",", list));
50+
51+
/* 通过索引遍历列表 */
52+
int count = 0;
53+
for (int i = 0; i < list.Count(); i++)
54+
{
55+
count++;
56+
}
57+
58+
/* 直接遍历列表元素 */
59+
count = 0;
60+
foreach (int n in list)
61+
{
62+
count++;
63+
}
64+
65+
/* 拼接两个列表 */
66+
List<int> list1 = new() { 6, 8, 7, 10, 9 };
67+
list.AddRange(list1);
68+
Console.WriteLine("将列表 list1 拼接到 list 之后,得到 list = " + string.Join(",", list));
69+
70+
/* 排序列表 */
71+
list.Sort(); // 排序后,列表元素从小到大排列
72+
Console.WriteLine("排序列表后 list = " + string.Join(",", list));
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)