forked from krahets/hello-algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'krahets:master' into rust-computational_complexity
- Loading branch information
Showing
135 changed files
with
4,948 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,9 @@ | |
# Editor | ||
.vscode/ | ||
.idea/ | ||
cmake-build-debug/ | ||
hello-algo.iml | ||
*.dSYM/ | ||
|
||
# mkdocs files | ||
site/ | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
FROM python:3.9.0-alpine | ||
|
||
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip | ||
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple mkdocs-material==9.0.2 | ||
|
||
WORKDIR /app | ||
|
||
COPY codes /app/codes | ||
COPY docs /app/docs | ||
COPY mkdocs.yml /app/mkdocs.yml | ||
|
||
RUN mkdir ./docs/overrides && mkdocs build | ||
|
||
EXPOSE 8000 | ||
|
||
CMD ["mkdocs", "serve", "-a", "0.0.0.0:8000"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
project(hello_algo C) | ||
|
||
set(CMAKE_C_STANDARD 11) | ||
|
||
include_directories(./include) | ||
|
||
add_subdirectory(include) | ||
add_subdirectory(chapter_computational_complexity) | ||
add_subdirectory(chapter_array_and_linkedlist) | ||
add_subdirectory(chapter_sorting) | ||
add_subdirectory(chapter_tree) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
add_executable(array array.c) | ||
add_executable(linked_list linked_list.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* File: linked_list.c | ||
* Created Time: 2022-01-12 | ||
* Author: Zero ([email protected]) | ||
*/ | ||
|
||
#include "../include/include.h" | ||
|
||
/* 在链表的结点 n0 之后插入结点 P */ | ||
void insert(ListNode* n0, ListNode* P) { | ||
ListNode *n1 = n0->next; | ||
n0->next = P; | ||
P->next = n1; | ||
} | ||
|
||
/* 删除链表的结点 n0 之后的首个结点 */ | ||
// 由于引入了 stdio.h ,此处无法使用 remove 关键词 | ||
// 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888 | ||
void removeNode(ListNode* n0) { | ||
if (!n0->next) | ||
return; | ||
// n0 -> P -> n1 | ||
ListNode *P = n0->next; | ||
ListNode *n1 = P->next; | ||
n0->next = n1; | ||
// 释放内存 | ||
free(P); | ||
} | ||
|
||
/* 访问链表中索引为 index 的结点 */ | ||
ListNode* access(ListNode* head, int index) { | ||
while (head && head->next && index) { | ||
head = head->next; | ||
index--; | ||
} | ||
return head; | ||
} | ||
|
||
/* 在链表中查找值为 target 的首个结点 */ | ||
int find(ListNode* head, int target) { | ||
int index = 0; | ||
while (head) { | ||
if (head->val == target) | ||
return index; | ||
head = head->next; | ||
index++; | ||
} | ||
return -1; | ||
} | ||
|
||
|
||
/* Driver Code */ | ||
int main() { | ||
/* 初始化链表 */ | ||
// 初始化各个结点 | ||
ListNode* n0 = newListNode(1); | ||
ListNode* n1 = newListNode(3); | ||
ListNode* n2 = newListNode(2); | ||
ListNode* n3 = newListNode(5); | ||
ListNode* n4 = newListNode(4); | ||
// 构建引用指向 | ||
n0->next = n1; | ||
n1->next = n2; | ||
n2->next = n3; | ||
n3->next = n4; | ||
printf("初始化的链表为\r\n"); | ||
printLinkedList(n0); | ||
|
||
/* 插入结点 */ | ||
insert(n0, newListNode(0)); | ||
printf("插入结点后的链表为\r\n"); | ||
printLinkedList(n0); | ||
|
||
/* 删除结点 */ | ||
removeNode(n0); | ||
printf("删除结点后的链表为\r\n"); | ||
printLinkedList(n0); | ||
|
||
/* 访问结点 */ | ||
ListNode* node = access(n0, 3); | ||
printf("链表中索引 3 处的结点的值 = %d\r\n", node->val); | ||
|
||
/* 查找结点 */ | ||
int index = find(n0, 2); | ||
printf("链表中值为 2 的结点的索引 = %d\r\n", index); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
add_executable(time_complexity time_complexity.c ) | ||
add_executable(worst_best_time_complexity worst_best_time_complexity.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,5 @@ int main(int argc, char *argv[]) { | |
nums = NULL; | ||
} | ||
} | ||
getchar(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
add_executable(bubble_sort bubble_sort.c) | ||
add_executable(insertion_sort insertion_sort.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
add_executable(binary_search binary_tree.c) | ||
add_executable(binary_tree_bfs binary_tree_bfs.c) | ||
add_executable(binary_tree_dfs binary_tree_dfs.c) | ||
add_executable(binary_search_tree binary_search_tree.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* File: binary_search_tree.c | ||
* Created Time: 2023-01-11 | ||
* Author: Reanon ([email protected]) | ||
*/ | ||
|
||
#include "../include/include.h" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* File: binary_tree.c | ||
* Created Time: 2023-01-11 | ||
* Author: Reanon ([email protected]) | ||
*/ | ||
|
||
#include "../include/include.h" | ||
|
||
/* Driver Code */ | ||
int main() { | ||
/* 初始化二叉树 */ | ||
// 初始化结点 | ||
TreeNode* n1 = newTreeNode(1); | ||
TreeNode* n2 = newTreeNode(2); | ||
TreeNode* n3 = newTreeNode(3); | ||
TreeNode* n4 = newTreeNode(4); | ||
TreeNode* n5 = newTreeNode(5); | ||
// 构建引用指向(即指针) | ||
n1->left = n2; | ||
n1->right = n3; | ||
n2->left = n4; | ||
n2->right = n5; | ||
printf("初始化二叉树\n"); | ||
printTree(n1); | ||
|
||
/* 插入与删除结点 */ | ||
TreeNode* P = newTreeNode(0); | ||
// 在 n1 -> n2 中间插入结点 P | ||
n1->left = P; | ||
P->left = n2; | ||
printf("插入结点 P 后\n"); | ||
printTree(n1); | ||
|
||
// 删除结点 P | ||
n1->left = n2; | ||
// 释放内存 | ||
free(P); | ||
printf("删除结点 P 后\n"); | ||
printTree(n1); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* File: binary_tree_bfs.c | ||
* Created Time: 2023-01-11 | ||
* Author: Reanon ([email protected]) | ||
*/ | ||
|
||
#include "../include/include.h" | ||
|
||
/* 层序遍历 */ | ||
int *levelOrder(TreeNode *root, int *size) { | ||
/* 辅助队列 */ | ||
int front, rear; | ||
int index, *arr; | ||
TreeNode *node; | ||
TreeNode **queue; | ||
|
||
/* 辅助队列 */ | ||
queue = (TreeNode **) malloc(sizeof(TreeNode) * MAX_NODE_SIZE); | ||
// 队列指针 | ||
front = 0, rear = 0; | ||
// 加入根结点 | ||
queue[rear++] = root; | ||
// 初始化一个列表,用于保存遍历序列 | ||
/* 辅助数组 */ | ||
arr = (int *) malloc(sizeof(int) * MAX_NODE_SIZE); | ||
// 数组指针 | ||
index = 0; | ||
while (front < rear) { | ||
// 队列出队 | ||
node = queue[front++]; | ||
// 保存结点 | ||
arr[index++] = node->val; | ||
if (node->left != NULL) { | ||
// 左子结点入队 | ||
queue[rear++] = node->left; | ||
} | ||
if (node->right != NULL) { | ||
// 右子结点入队 | ||
queue[rear++] = node->right; | ||
} | ||
} | ||
// 更新数组长度的值 | ||
*size = index; | ||
arr = realloc(arr, sizeof(int) * (*size)); | ||
return arr; | ||
} | ||
|
||
|
||
/* Driver Code */ | ||
int main() { | ||
/* 初始化二叉树 */ | ||
// 这里借助了一个从数组直接生成二叉树的函数 | ||
int nums[] = {1, 2, 3, NIL, 5, 6, NIL}; | ||
int size = sizeof(nums) / sizeof(int); | ||
TreeNode *root = arrToTree(nums, size); | ||
printf("初始化二叉树\n"); | ||
printTree(root); | ||
|
||
/* 层序遍历 */ | ||
// 需要传入数组的长度 | ||
int *arr = levelOrder(root, &size); | ||
printf("层序遍历的结点打印序列 = "); | ||
printArray(arr, size); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.