diff --git a/codes/c/.gitignore b/codes/c/.gitignore index 201eb6b96..698ee4e21 100644 --- a/codes/c/.gitignore +++ b/codes/c/.gitignore @@ -6,4 +6,4 @@ !*/ *.dSYM/ -build/ \ No newline at end of file +build/ diff --git a/codes/c/chapter_backtracking/subset_sum_ii.c b/codes/c/chapter_backtracking/subset_sum_ii.c index 54670d525..7e9e8ca13 100644 --- a/codes/c/chapter_backtracking/subset_sum_ii.c +++ b/codes/c/chapter_backtracking/subset_sum_ii.c @@ -46,10 +46,10 @@ int comp(const void *a, const void *b) { /* 求解子集和 II */ vector *subsetSumII(vector *nums, int target) { - vector *state = newVector(); // 状态(子集) + vector *state = newVector(); // 状态(子集) qsort(nums->data, nums->size, sizeof(int *), comp); // 对 nums 进行排序 - int start = 0; // 子集和 - vector *res = newVector(); // 结果列表(子集列表) + int start = 0; // 子集和 + vector *res = newVector(); // 结果列表(子集列表) backtrack(state, target, nums, start, res); return res; } diff --git a/codes/c/chapter_computational_complexity/time_complexity.c b/codes/c/chapter_computational_complexity/time_complexity.c index 39e7942ff..3f1986f0a 100644 --- a/codes/c/chapter_computational_complexity/time_complexity.c +++ b/codes/c/chapter_computational_complexity/time_complexity.c @@ -53,7 +53,7 @@ int bubbleSort(int *nums, int n) { int count = 0; // 计数器 // 外循环:未排序区间为 [0, i] for (int i = n - 1; i > 0; i--) { - // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/c/chapter_graph/graph_adjacency_list.c b/codes/c/chapter_graph/graph_adjacency_list.c index 758ef5875..777a97ada 100644 --- a/codes/c/chapter_graph/graph_adjacency_list.c +++ b/codes/c/chapter_graph/graph_adjacency_list.c @@ -230,7 +230,7 @@ void removeVertex(graphAdjList *t, unsigned int index) { Node *temp = vet->linked->head->next; while (temp != 0) { removeLink(temp->val->linked, vet); // 删除与该顶点有关的边 - temp = temp->next; + temp = temp->next; } // 将顶点前移 @@ -241,7 +241,7 @@ void removeVertex(graphAdjList *t, unsigned int index) { t->verticesList[t->size - 1] = 0; // 将被删除顶点的位置置 0 t->size--; - //释放被删除顶点的内存 + // 释放内存 freeVertex(vet); } @@ -273,5 +273,5 @@ graphAdjList *newGraphAdjList(unsigned int verticesCapacity) { newGraph->size = 0; // 初始化顶点数量 newGraph->capacity = verticesCapacity; // 初始化顶点容量 // 返回图指针 - return newGraph; + return newGraph; } diff --git a/codes/c/chapter_graph/graph_adjacency_matrix.c b/codes/c/chapter_graph/graph_adjacency_matrix.c index 8667e0010..798a8e4d0 100644 --- a/codes/c/chapter_graph/graph_adjacency_matrix.c +++ b/codes/c/chapter_graph/graph_adjacency_matrix.c @@ -49,8 +49,6 @@ void addVertex(graphAdjMat *t, int val) { // 如果实际使用不大于预设空间,则直接初始化新空间 if (t->size < t->capacity) { t->vertices[t->size] = val; // 初始化新顶点值 - - for (int i = 0; i < t->size; i++) { t->adjMat[i][t->size] = 0; // 邻接矩新列阵置0 } @@ -90,7 +88,7 @@ void addVertex(graphAdjMat *t, int val) { free(t->adjMat); // 扩容后,指向新地址 - t->adjMat = tempMat; // 指向新的邻接矩阵地址 + t->adjMat = tempMat; // 指向新的邻接矩阵地址 t->capacity = t->size * 2; t->size++; } @@ -113,7 +111,7 @@ void removeVertex(graphAdjMat *t, unsigned int index) { for (int j = index; j < t->size - 1; j++) { t->adjMat[i][j] = t->adjMat[i][j + 1]; // 被删除列后的所有列前移 } - } else { + } else { memcpy(t->adjMat[i], t->adjMat[i + 1], sizeof(unsigned int) * t->size); // 被删除行的下方所有行上移 for (int j = index; j < t->size; j++) { t->adjMat[i][j] = t->adjMat[i][j + 1]; // 被删除列后的所有列前移 @@ -156,12 +154,12 @@ void printGraph(graphAdjMat *t) { /* 构造函数 */ graphAdjMat *newGraphAjdMat(unsigned int numberVertices, int *vertices, unsigned int **adjMat) { // 申请内存 - graphAdjMat *newGraph = (graphAdjMat *)malloc(sizeof(graphAdjMat)); // 为图分配内存 - newGraph->vertices = (int *)malloc(sizeof(int) * numberVertices * 2); // 为顶点列表分配内存 - newGraph->adjMat = (unsigned int **)malloc(sizeof(unsigned int *) * numberVertices * 2); // 为邻接矩阵分配二维内存 + graphAdjMat *newGraph = (graphAdjMat *)malloc(sizeof(graphAdjMat)); // 为图分配内存 + newGraph->vertices = (int *)malloc(sizeof(int) * numberVertices * 2); // 为顶点列表分配内存 + newGraph->adjMat = (unsigned int **)malloc(sizeof(unsigned int *) * numberVertices * 2); // 为邻接矩阵分配二维内存 unsigned int *temp = (unsigned int *)malloc(sizeof(unsigned int) * numberVertices * 2 * numberVertices * 2); // 为邻接矩阵分配一维内存 - newGraph->size = numberVertices; // 初始化顶点数量 - newGraph->capacity = numberVertices * 2; // 初始化图容量 + newGraph->size = numberVertices; // 初始化顶点数量 + newGraph->capacity = numberVertices * 2; // 初始化图容量 // 配置二维数组 for (int i = 0; i < numberVertices * 2; i++) { diff --git a/codes/c/chapter_hashing/array_hash_map.c b/codes/c/chapter_hashing/array_hash_map.c index e55f4dbbb..d5cb87659 100644 --- a/codes/c/chapter_hashing/array_hash_map.c +++ b/codes/c/chapter_hashing/array_hash_map.c @@ -7,7 +7,7 @@ #include "../utils/common.h" /* 哈希表默认数组大小 */ -# define HASH_MAP_DEFAULT_SIZE 100 +#define HASH_MAP_DEFAULT_SIZE 100 /* 键值对 int->string */ struct pair { @@ -154,7 +154,7 @@ void print(ArrayHashMap *d) { int i; mapSet set; pairSet(d, &set); - pair *entries = (pair*) set.set; + pair *entries = (pair *)set.set; for (i = 0; i < set.len; i++) { printf("%d -> %s\n", entries[i].key, entries[i].val); } @@ -164,7 +164,7 @@ void print(ArrayHashMap *d) { /* Driver Code */ int main() { /* 初始化哈希表 */ - ArrayHashMap * map = newArrayHashMap(); + ArrayHashMap *map = newArrayHashMap(); /* 添加操作 */ // 在哈希表中添加键值对 (key, value) @@ -178,7 +178,7 @@ int main() { /* 查询操作 */ // 向哈希表输入键 key ,得到值 value - const char * name = get(map, 15937); + const char *name = get(map, 15937); printf("\n输入学号 15937 ,查询到姓名 %s\n", name); /* 删除操作 */ @@ -196,7 +196,7 @@ int main() { mapSet set; keySet(map, &set); - int *keys = (int*) set.set; + int *keys = (int *)set.set; printf("\n单独遍历键 Key\n"); for (i = 0; i < set.len; i++) { printf("%d\n", keys[i]); @@ -204,12 +204,12 @@ int main() { free(set.set); valueSet(map, &set); - char **vals = (char**) set.set; + char **vals = (char **)set.set; printf("\n单独遍历键 Value\n"); for (i = 0; i < set.len; i++) { printf("%s\n", vals[i]); } free(set.set); - + return 0; } diff --git a/codes/c/chapter_sorting/bubble_sort.c b/codes/c/chapter_sorting/bubble_sort.c index bebd41fbc..6df1d1858 100644 --- a/codes/c/chapter_sorting/bubble_sort.c +++ b/codes/c/chapter_sorting/bubble_sort.c @@ -10,7 +10,7 @@ void bubbleSort(int nums[], int size) { // 外循环:未排序区间为 [0, i] for (int i = 0; i < size - 1; i++) { - // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < size - 1 - i; j++) { if (nums[j] > nums[j + 1]) { int temp = nums[j]; @@ -26,7 +26,7 @@ void bubbleSortWithFlag(int nums[], int size) { // 外循环:未排序区间为 [0, i] for (int i = 0; i < size - 1; i++) { bool flag = false; - // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < size - 1 - i; j++) { if (nums[j] > nums[j + 1]) { int temp = nums[j]; diff --git a/codes/c/chapter_sorting/selection_sort.c b/codes/c/chapter_sorting/selection_sort.c index d80d804ca..d6c05ab79 100644 --- a/codes/c/chapter_sorting/selection_sort.c +++ b/codes/c/chapter_sorting/selection_sort.c @@ -14,7 +14,7 @@ void selectionSort(int nums[], int n) { int k = i; for (int j = i + 1; j < n; j++) { if (nums[j] < nums[k]) - k = j; // 记录最小元素的索引 + k = j; // 记录最小元素的索引 } // 将该最小元素与未排序区间的首个元素交换 int temp = nums[i]; diff --git a/codes/cpp/chapter_computational_complexity/time_complexity.cpp b/codes/cpp/chapter_computational_complexity/time_complexity.cpp index c4fb1fd28..34f73c035 100644 --- a/codes/cpp/chapter_computational_complexity/time_complexity.cpp +++ b/codes/cpp/chapter_computational_complexity/time_complexity.cpp @@ -50,7 +50,7 @@ int bubbleSort(vector &nums) { int count = 0; // 计数器 // 外循环:未排序区间为 [0, i] for (int i = nums.size() - 1; i > 0; i--) { - // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/cpp/chapter_divide_and_conquer/hanota.cpp b/codes/cpp/chapter_divide_and_conquer/hanota.cpp index 308982a0a..a27bbe516 100644 --- a/codes/cpp/chapter_divide_and_conquer/hanota.cpp +++ b/codes/cpp/chapter_divide_and_conquer/hanota.cpp @@ -31,7 +31,7 @@ void dfs(int i, vector &src, vector &buf, vector &tar) { } /* 求解汉诺塔 */ -void hanota(vector &A, vector &B, vector &C) { +void solveHanota(vector &A, vector &B, vector &C) { int n = A.size(); // 将 A 顶部 n 个圆盘借助 B 移到 C dfs(n, A, B, C); @@ -52,7 +52,7 @@ int main() { cout << "C ="; printVector(C); - hanota(A, B, C); + solveHanota(A, B, C); cout << "圆盘移动完成后:\n"; cout << "A ="; diff --git a/codes/cpp/chapter_hashing/hash_map_chaining.cpp b/codes/cpp/chapter_hashing/hash_map_chaining.cpp index 012ad9abc..a61703724 100644 --- a/codes/cpp/chapter_hashing/hash_map_chaining.cpp +++ b/codes/cpp/chapter_hashing/hash_map_chaining.cpp @@ -20,7 +20,7 @@ class HashMapChaining { HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3), extendRatio(2) { buckets.resize(capacity); } - + /* 析构方法 */ ~HashMapChaining() { for (auto &bucket : buckets) { diff --git a/codes/cpp/chapter_hashing/simple_hash.cpp b/codes/cpp/chapter_hashing/simple_hash.cpp index f32e13180..66dc8b406 100644 --- a/codes/cpp/chapter_hashing/simple_hash.cpp +++ b/codes/cpp/chapter_hashing/simple_hash.cpp @@ -31,7 +31,7 @@ int xorHash(string key) { int hash = 0; const int MODULUS = 1000000007; for (unsigned char c : key) { - cout<<(int)c< &nums) { // 外循环:未排序区间为 [0, i] for (int i = nums.size() - 1; i > 0; i--) { - // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] @@ -26,7 +26,7 @@ void bubbleSortWithFlag(vector &nums) { // 外循环:未排序区间为 [0, i] for (int i = nums.size() - 1; i > 0; i--) { bool flag = false; // 初始化标志位 - // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/csharp/chapter_backtracking/subset_sum_i.cs b/codes/csharp/chapter_backtracking/subset_sum_i.cs index 1840e074a..f15706351 100644 --- a/codes/csharp/chapter_backtracking/subset_sum_i.cs +++ b/codes/csharp/chapter_backtracking/subset_sum_i.cs @@ -4,7 +4,7 @@ * Author: hpstory (hpstory1024@163.com) */ -namespace hello_algo.chapter_backtracking; +namespace hello_algo.chapter_backtracking; public class subset_sum_i { /* 回溯算法:子集和 I */ diff --git a/codes/csharp/chapter_backtracking/subset_sum_i_naive.cs b/codes/csharp/chapter_backtracking/subset_sum_i_naive.cs index 6e16ed78c..34ca5ff4a 100644 --- a/codes/csharp/chapter_backtracking/subset_sum_i_naive.cs +++ b/codes/csharp/chapter_backtracking/subset_sum_i_naive.cs @@ -4,7 +4,7 @@ * Author: hpstory (hpstory1024@163.com) */ -namespace hello_algo.chapter_backtracking; +namespace hello_algo.chapter_backtracking; public class subset_sum_i_naive { /* 回溯算法:子集和 I */ diff --git a/codes/csharp/chapter_backtracking/subset_sum_ii.cs b/codes/csharp/chapter_backtracking/subset_sum_ii.cs index dec1ef317..11822fa69 100644 --- a/codes/csharp/chapter_backtracking/subset_sum_ii.cs +++ b/codes/csharp/chapter_backtracking/subset_sum_ii.cs @@ -4,7 +4,7 @@ * Author: hpstory (hpstory1024@163.com) */ -namespace hello_algo.chapter_backtracking; +namespace hello_algo.chapter_backtracking; public class subset_sum_ii { /* 回溯算法:子集和 II */ diff --git a/codes/csharp/chapter_dynamic_programming/climbing_stairs_backtrack.cs b/codes/csharp/chapter_dynamic_programming/climbing_stairs_backtrack.cs index 88f8c028a..9fa819d07 100644 --- a/codes/csharp/chapter_dynamic_programming/climbing_stairs_backtrack.cs +++ b/codes/csharp/chapter_dynamic_programming/climbing_stairs_backtrack.cs @@ -4,7 +4,7 @@ * Author: hpstory (hpstory1024@163.com) */ -namespace hello_algo.chapter_dynamic_programming; +namespace hello_algo.chapter_dynamic_programming; public class climbing_stairs_backtrack { /* 回溯 */ diff --git a/codes/csharp/chapter_dynamic_programming/climbing_stairs_dfs.cs b/codes/csharp/chapter_dynamic_programming/climbing_stairs_dfs.cs index 29dd91940..34c36c3d8 100644 --- a/codes/csharp/chapter_dynamic_programming/climbing_stairs_dfs.cs +++ b/codes/csharp/chapter_dynamic_programming/climbing_stairs_dfs.cs @@ -4,7 +4,7 @@ * Author: hpstory (hpstory1024@163.com) */ -namespace hello_algo.chapter_dynamic_programming; +namespace hello_algo.chapter_dynamic_programming; public class climbing_stairs_dfs { /* 搜索 */ diff --git a/codes/csharp/chapter_dynamic_programming/climbing_stairs_dp.cs b/codes/csharp/chapter_dynamic_programming/climbing_stairs_dp.cs index 90cca913c..7ad3b5a41 100644 --- a/codes/csharp/chapter_dynamic_programming/climbing_stairs_dp.cs +++ b/codes/csharp/chapter_dynamic_programming/climbing_stairs_dp.cs @@ -4,7 +4,7 @@ * Author: hpstory (hpstory1024@163.com) */ -namespace hello_algo.chapter_dynamic_programming; +namespace hello_algo.chapter_dynamic_programming; public class climbing_stairs_dp { /* 爬楼梯:动态规划 */ diff --git a/codes/csharp/chapter_dynamic_programming/min_path_sum.cs b/codes/csharp/chapter_dynamic_programming/min_path_sum.cs index 1a534aaac..c6809c074 100644 --- a/codes/csharp/chapter_dynamic_programming/min_path_sum.cs +++ b/codes/csharp/chapter_dynamic_programming/min_path_sum.cs @@ -4,13 +4,13 @@ * Author: hpstory (hpstory1024@163.com) */ -namespace hello_algo.chapter_dynamic_programming; +namespace hello_algo.chapter_dynamic_programming; public class min_path_sum { /* 最小路径和:暴力搜索 */ public int minPathSumDFS(int[][] grid, int i, int j) { // 若为左上角单元格,则终止搜索 - if (i == 0 && j == 0){ + if (i == 0 && j == 0) { return grid[0][0]; } // 若行列索引越界,则返回 +∞ 代价 diff --git a/codes/csharp/chapter_hashing/built_in_hash.cs b/codes/csharp/chapter_hashing/built_in_hash.cs index 062a5e8d5..4b310271a 100644 --- a/codes/csharp/chapter_hashing/built_in_hash.cs +++ b/codes/csharp/chapter_hashing/built_in_hash.cs @@ -4,7 +4,7 @@ * Author: hpstory (hpstory1024@163.com) */ -namespace hello_algo.chapter_hashing; +namespace hello_algo.chapter_hashing; public class built_in_hash { [Test] diff --git a/codes/csharp/chapter_hashing/simple_hash.cs b/codes/csharp/chapter_hashing/simple_hash.cs index 84542c885..717a1d3da 100644 --- a/codes/csharp/chapter_hashing/simple_hash.cs +++ b/codes/csharp/chapter_hashing/simple_hash.cs @@ -4,7 +4,7 @@ * Author: hpstory (hpstory1024@163.com) */ -namespace hello_algo.chapter_hashing; +namespace hello_algo.chapter_hashing; public class simple_hash { /* 加法哈希 */ diff --git a/codes/csharp/chapter_heap/top_k.cs b/codes/csharp/chapter_heap/top_k.cs index 842090416..f073e74af 100644 --- a/codes/csharp/chapter_heap/top_k.cs +++ b/codes/csharp/chapter_heap/top_k.cs @@ -4,7 +4,7 @@ * Author: hpstory (hpstory1024@163.com) */ -namespace hello_algo.chapter_heap; +namespace hello_algo.chapter_heap; public class top_k { /* 基于堆查找数组中最大的 k 个元素 */ diff --git a/codes/csharp/chapter_sorting/bucket_sort.cs b/codes/csharp/chapter_sorting/bucket_sort.cs index 6b6d0de48..fe2a497ab 100644 --- a/codes/csharp/chapter_sorting/bucket_sort.cs +++ b/codes/csharp/chapter_sorting/bucket_sort.cs @@ -18,7 +18,7 @@ public class bucket_sort { // 1. 将数组元素分配到各个桶中 foreach (float num in nums) { // 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1] - int i = (int) (num * k); + int i = (int)(num * k); // 将 num 添加进桶 i buckets[i].Add(num); } diff --git a/codes/csharp/chapter_sorting/heap_sort.cs b/codes/csharp/chapter_sorting/heap_sort.cs index 47a87241d..56c5d30c0 100644 --- a/codes/csharp/chapter_sorting/heap_sort.cs +++ b/codes/csharp/chapter_sorting/heap_sort.cs @@ -4,7 +4,7 @@ * Author: hpstory (hpstory1024@163.com) */ -namespace hello_algo.chapter_sorting; +namespace hello_algo.chapter_sorting; public class heap_sort { /* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */ diff --git a/codes/csharp/utils/TreeNode.cs b/codes/csharp/utils/TreeNode.cs index d9752e22e..18cbfa213 100644 --- a/codes/csharp/utils/TreeNode.cs +++ b/codes/csharp/utils/TreeNode.cs @@ -36,7 +36,7 @@ public class TreeNode { /* 将列表反序列化为二叉树:递归 */ private static TreeNode? ListToTreeDFS(List arr, int i) { - if (i < 0|| i >= arr.Count || !arr[i].HasValue) { + if (i < 0 || i >= arr.Count || !arr[i].HasValue) { return null; } TreeNode root = new TreeNode(arr[i].Value); diff --git a/codes/dart/chapter_divide_and_conquer/hanota.dart b/codes/dart/chapter_divide_and_conquer/hanota.dart index bbc4f1324..ff3f33c4f 100644 --- a/codes/dart/chapter_divide_and_conquer/hanota.dart +++ b/codes/dart/chapter_divide_and_conquer/hanota.dart @@ -28,7 +28,7 @@ void dfs(int i, List src, List buf, List tar) { } /* 求解汉诺塔 */ -void hanota(List A, List B, List C) { +void solveHanota(List A, List B, List C) { int n = A.length; // 将 A 顶部 n 个圆盘借助 B 移到 C dfs(n, A, B, C); @@ -45,7 +45,7 @@ void main() { print("B = $B"); print("C = $C"); - hanota(A, B, C); + solveHanota(A, B, C); print("圆盘移动完成后:"); print("A = $A"); diff --git a/codes/go/chapter_array_and_linkedlist/list_test.go b/codes/go/chapter_array_and_linkedlist/list_test.go index 141f83ad7..36fb88d8d 100644 --- a/codes/go/chapter_array_and_linkedlist/list_test.go +++ b/codes/go/chapter_array_and_linkedlist/list_test.go @@ -64,4 +64,4 @@ func TestList(t *testing.T) { /* 排序列表 */ sort.Ints(list) // 排序后,列表元素从小到大排列 fmt.Println("排序列表后 list =", list) -} \ No newline at end of file +} diff --git a/codes/go/chapter_array_and_linkedlist/my_list_test.go b/codes/go/chapter_array_and_linkedlist/my_list_test.go index afe16e4df..765041f94 100644 --- a/codes/go/chapter_array_and_linkedlist/my_list_test.go +++ b/codes/go/chapter_array_and_linkedlist/my_list_test.go @@ -43,4 +43,4 @@ func TestMyList(t *testing.T) { list.add(i) } fmt.Printf("扩容后的列表 list = %v ,容量 = %v ,长度 = %v\n", list.toArray(), list.capacity(), list.size()) -} \ No newline at end of file +} diff --git a/codes/go/chapter_divide_and_conquer/build_tree_test.go b/codes/go/chapter_divide_and_conquer/build_tree_test.go index bffc693c6..ab6150634 100644 --- a/codes/go/chapter_divide_and_conquer/build_tree_test.go +++ b/codes/go/chapter_divide_and_conquer/build_tree_test.go @@ -6,8 +6,9 @@ package chapter_divide_and_conquer import ( "fmt" - . "github.com/krahets/hello-algo/pkg" "testing" + + . "github.com/krahets/hello-algo/pkg" ) func TestBuildTree(t *testing.T) { diff --git a/codes/go/chapter_divide_and_conquer/hanota.go b/codes/go/chapter_divide_and_conquer/hanota.go index 89fba0ffa..8b01113fd 100644 --- a/codes/go/chapter_divide_and_conquer/hanota.go +++ b/codes/go/chapter_divide_and_conquer/hanota.go @@ -32,7 +32,7 @@ func dfsHanota(i int, src, buf, tar *list.List) { } /* 求解汉诺塔 */ -func hanota(A, B, C *list.List) { +func solveHanota(A, B, C *list.List) { n := A.Len() // 将 A 顶部 n 个圆盘借助 B 移到 C dfsHanota(n, A, B, C) diff --git a/codes/go/chapter_divide_and_conquer/hanota_test.go b/codes/go/chapter_divide_and_conquer/hanota_test.go index 1dd955bbc..1018ecb9a 100644 --- a/codes/go/chapter_divide_and_conquer/hanota_test.go +++ b/codes/go/chapter_divide_and_conquer/hanota_test.go @@ -7,8 +7,9 @@ package chapter_divide_and_conquer import ( "container/list" "fmt" - . "github.com/krahets/hello-algo/pkg" "testing" + + . "github.com/krahets/hello-algo/pkg" ) func TestHanota(t *testing.T) { @@ -27,7 +28,7 @@ func TestHanota(t *testing.T) { fmt.Print("C = ") PrintList(C) - hanota(A, B, C) + solveHanota(A, B, C) fmt.Println("圆盘移动完成后:") fmt.Print("A = ") diff --git a/codes/java/chapter_computational_complexity/time_complexity.java b/codes/java/chapter_computational_complexity/time_complexity.java index 121ae9685..a9bf97fe9 100644 --- a/codes/java/chapter_computational_complexity/time_complexity.java +++ b/codes/java/chapter_computational_complexity/time_complexity.java @@ -51,7 +51,7 @@ public class time_complexity { int count = 0; // 计数器 // 外循环:未排序区间为 [0, i] for (int i = nums.length - 1; i > 0; i--) { - // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/java/chapter_sorting/bubble_sort.java b/codes/java/chapter_sorting/bubble_sort.java index ca03a9c12..86b05de56 100644 --- a/codes/java/chapter_sorting/bubble_sort.java +++ b/codes/java/chapter_sorting/bubble_sort.java @@ -13,7 +13,7 @@ public class bubble_sort { static void bubbleSort(int[] nums) { // 外循环:未排序区间为 [0, i] for (int i = nums.length - 1; i > 0; i--) { - // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] @@ -30,7 +30,7 @@ public class bubble_sort { // 外循环:未排序区间为 [0, i] for (int i = nums.length - 1; i > 0; i--) { boolean flag = false; // 初始化标志位 - // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/javascript/chapter_divide_and_conquer/hanota.js b/codes/javascript/chapter_divide_and_conquer/hanota.js index 92d3b6abf..009d43253 100644 --- a/codes/javascript/chapter_divide_and_conquer/hanota.js +++ b/codes/javascript/chapter_divide_and_conquer/hanota.js @@ -28,7 +28,7 @@ function dfs(i, src, buf, tar) { } /* 求解汉诺塔 */ -function hanota(A, B, C) { +function solveHanota(A, B, C) { const n = A.length; // 将 A 顶部 n 个圆盘借助 B 移到 C dfs(n, A, B, C); @@ -44,7 +44,7 @@ console.log(`A = ${JSON.stringify(A)}`); console.log(`B = ${JSON.stringify(B)}`); console.log(`C = ${JSON.stringify(C)}`); -hanota(A, B, C); +solveHanota(A, B, C); console.log('圆盘移动完成后:'); console.log(`A = ${JSON.stringify(A)}`); diff --git a/codes/python/chapter_computational_complexity/time_complexity.py b/codes/python/chapter_computational_complexity/time_complexity.py index 7ea3b4830..79240e01e 100644 --- a/codes/python/chapter_computational_complexity/time_complexity.py +++ b/codes/python/chapter_computational_complexity/time_complexity.py @@ -46,7 +46,7 @@ def bubble_sort(nums: list[int]) -> int: count = 0 # 计数器 # 外循环:未排序区间为 [0, i] for i in range(len(nums) - 1, 0, -1): - # 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + # 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for j in range(i): if nums[j] > nums[j + 1]: # 交换 nums[j] 与 nums[j + 1] diff --git a/codes/python/chapter_divide_and_conquer/hanota.py b/codes/python/chapter_divide_and_conquer/hanota.py index 0ae0d1979..34cfd93d1 100644 --- a/codes/python/chapter_divide_and_conquer/hanota.py +++ b/codes/python/chapter_divide_and_conquer/hanota.py @@ -27,7 +27,7 @@ def dfs(i: int, src: list[int], buf: list[int], tar: list[int]): dfs(i - 1, buf, src, tar) -def hanota(A: list[int], B: list[int], C: list[int]): +def solve_hanota(A: list[int], B: list[int], C: list[int]): """求解汉诺塔""" n = len(A) # 将 A 顶部 n 个圆盘借助 B 移到 C @@ -45,7 +45,7 @@ if __name__ == "__main__": print(f"B = {B}") print(f"C = {C}") - hanota(A, B, C) + solve_hanota(A, B, C) print("圆盘移动完成后:") print(f"A = {A}") diff --git a/codes/python/chapter_hashing/built_in_hash.py b/codes/python/chapter_hashing/built_in_hash.py index d17d5e3b6..bdb0b1e35 100644 --- a/codes/python/chapter_hashing/built_in_hash.py +++ b/codes/python/chapter_hashing/built_in_hash.py @@ -20,7 +20,7 @@ if __name__ == "__main__": print(f"布尔量 {bol} 的哈希值为 {hash_bol}") dec = 3.14159 - hash_dec = hash(dec) + hash_dec = hash(dec) print(f"小数 {dec} 的哈希值为 {hash_dec}") str = "Hello 算法" diff --git a/codes/python/chapter_heap/top_k.py b/codes/python/chapter_heap/top_k.py index 2ee2e6e68..5c6cae716 100644 --- a/codes/python/chapter_heap/top_k.py +++ b/codes/python/chapter_heap/top_k.py @@ -33,5 +33,5 @@ if __name__ == "__main__": k = 3 res = top_k_heap(nums, k) - print(f"最大的 {k} 个元素为") + print(f"最大的 {k} 个元素为") print_heap(res) diff --git a/codes/python/chapter_sorting/bubble_sort.py b/codes/python/chapter_sorting/bubble_sort.py index c0406e0d2..238444e2b 100644 --- a/codes/python/chapter_sorting/bubble_sort.py +++ b/codes/python/chapter_sorting/bubble_sort.py @@ -10,7 +10,7 @@ def bubble_sort(nums: list[int]): n = len(nums) # 外循环:未排序区间为 [0, i] for i in range(n - 1, 0, -1): - # 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + # 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for j in range(i): if nums[j] > nums[j + 1]: # 交换 nums[j] 与 nums[j + 1] @@ -23,7 +23,7 @@ def bubble_sort_with_flag(nums: list[int]): # 外循环:未排序区间为 [0, i] for i in range(n - 1, 0, -1): flag = False # 初始化标志位 - # 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + # 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for j in range(i): if nums[j] > nums[j + 1]: # 交换 nums[j] 与 nums[j + 1] diff --git a/codes/python/chapter_sorting/heap_sort.py b/codes/python/chapter_sorting/heap_sort.py index 7577d1357..405d75f87 100644 --- a/codes/python/chapter_sorting/heap_sort.py +++ b/codes/python/chapter_sorting/heap_sort.py @@ -36,7 +36,7 @@ def heap_sort(nums: list[int]): nums[0], nums[i] = nums[i], nums[0] # 以根节点为起点,从顶至底进行堆化 sift_down(nums, i, 0) - + """Driver Code""" if __name__ == "__main__": diff --git a/codes/python/modules/print_util.py b/codes/python/modules/print_util.py index 397eb136a..71bd949cc 100644 --- a/codes/python/modules/print_util.py +++ b/codes/python/modules/print_util.py @@ -35,9 +35,7 @@ def show_trunks(p: Trunk | None): print(p.str, end="") -def print_tree( - root: TreeNode | None, prev: Trunk | None = None, is_left: bool = False -): +def print_tree(root: TreeNode | None, prev: Trunk | None = None, is_left: bool = False): """ Print a binary tree This tree printer is borrowed from TECHIE DELIGHT diff --git a/codes/rust/chapter_array_and_linkedlist/list.rs b/codes/rust/chapter_array_and_linkedlist/list.rs index 63ef409d8..2f3140b20 100644 --- a/codes/rust/chapter_array_and_linkedlist/list.rs +++ b/codes/rust/chapter_array_and_linkedlist/list.rs @@ -8,68 +8,68 @@ include!("../include/include.rs"); /* Driver Code */ fn main() { - // 初始化列表 - let mut list: Vec = vec![ 1, 3, 2, 5, 4 ]; - print!("列表 list = "); - print_util::print_array(&list); + // 初始化列表 + let mut list: Vec = vec![ 1, 3, 2, 5, 4 ]; + print!("列表 list = "); + print_util::print_array(&list); - // 访问元素 - let num = list[1]; - println!("\n访问索引 1 处的元素,得到 num = {num}"); + // 访问元素 + let num = list[1]; + println!("\n访问索引 1 处的元素,得到 num = {num}"); - // 更新元素 - list[1] = 0; - print!("将索引 1 处的元素更新为 0 ,得到 list = "); - print_util::print_array(&list); + // 更新元素 + list[1] = 0; + print!("将索引 1 处的元素更新为 0 ,得到 list = "); + print_util::print_array(&list); - // 清空列表 - list.clear(); - print!("\n清空列表后 list = "); - print_util::print_array(&list); + // 清空列表 + list.clear(); + print!("\n清空列表后 list = "); + print_util::print_array(&list); - // 尾部添加元素 - list.push(1); - list.push(3); - list.push(2); - list.push(5); - list.push(4); - print!("\n添加元素后 list = "); - print_util::print_array(&list); + // 尾部添加元素 + list.push(1); + list.push(3); + list.push(2); + list.push(5); + list.push(4); + print!("\n添加元素后 list = "); + print_util::print_array(&list); - // 中间插入元素 - list.insert(3, 6); - print!("\n在索引 3 处插入数字 6 ,得到 list = "); - print_util::print_array(&list); + // 中间插入元素 + list.insert(3, 6); + print!("\n在索引 3 处插入数字 6 ,得到 list = "); + print_util::print_array(&list); - // 删除元素 - list.remove(3); - print!("\n删除索引 3 处的元素,得到 list = "); - print_util::print_array(&list); + // 删除元素 + list.remove(3); + print!("\n删除索引 3 处的元素,得到 list = "); + print_util::print_array(&list); - // 通过索引遍历列表 - let mut _count = 0; - for _ in 0..list.len() { - _count += 1; - } + // 通过索引遍历列表 + let mut _count = 0; + for _ in 0..list.len() { + _count += 1; + } - // 直接遍历列表元素 - _count = 0; - for _n in &list { - _count += 1; - } - // 或者 - // list.iter().for_each(|_| _count += 1); - // let _count = list.iter().fold(0, |_count, _| _count + 1); + // 直接遍历列表元素 + _count = 0; + for _n in &list { + _count += 1; + } + // 或者 + // list.iter().for_each(|_| _count += 1); + // let _count = list.iter().fold(0, |_count, _| _count + 1); - // 拼接两个列表 - let mut list1 = vec![ 6, 8, 7, 10, 9 ]; - list.append(&mut list1); // append(移动) 之后 list1 为空! - // list.extend(&list1); // extend(借用) list1 能继续使用 - print!("\n将列表 list1 拼接到 list 之后,得到 list = "); - print_util::print_array(&list); + // 拼接两个列表 + let mut list1 = vec![ 6, 8, 7, 10, 9 ]; + list.append(&mut list1); // append(移动) 之后 list1 为空! + // list.extend(&list1); // extend(借用) list1 能继续使用 + print!("\n将列表 list1 拼接到 list 之后,得到 list = "); + print_util::print_array(&list); - // 排序列表 - list.sort(); - print!("\n排序列表后 list = "); - print_util::print_array(&list); + // 排序列表 + list.sort(); + print!("\n排序列表后 list = "); + print_util::print_array(&list); } diff --git a/codes/rust/chapter_backtracking/preorder_traversal_i_compact.rs b/codes/rust/chapter_backtracking/preorder_traversal_i_compact.rs index 0e618228c..53522cdd8 100644 --- a/codes/rust/chapter_backtracking/preorder_traversal_i_compact.rs +++ b/codes/rust/chapter_backtracking/preorder_traversal_i_compact.rs @@ -4,7 +4,7 @@ * Author: sjinzh (sjinzh@gmail.com) */ - include!("../include/include.rs"); +include!("../include/include.rs"); use std::{cell::RefCell, rc::Rc}; use tree_node::{vec_to_tree, TreeNode}; diff --git a/codes/rust/chapter_backtracking/preorder_traversal_ii_compact.rs b/codes/rust/chapter_backtracking/preorder_traversal_ii_compact.rs index 920cfab0e..57ccea439 100644 --- a/codes/rust/chapter_backtracking/preorder_traversal_ii_compact.rs +++ b/codes/rust/chapter_backtracking/preorder_traversal_ii_compact.rs @@ -4,7 +4,7 @@ * Author: sjinzh (sjinzh@gmail.com) */ - include!("../include/include.rs"); +include!("../include/include.rs"); use std::{cell::RefCell, rc::Rc}; use tree_node::{vec_to_tree, TreeNode}; diff --git a/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs b/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs index ae28d3f88..f88662f46 100644 --- a/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs +++ b/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs @@ -4,7 +4,7 @@ * Author: sjinzh (sjinzh@gmail.com) */ - include!("../include/include.rs"); +include!("../include/include.rs"); use std::{cell::RefCell, rc::Rc}; use tree_node::{vec_to_tree, TreeNode}; diff --git a/codes/rust/chapter_backtracking/preorder_traversal_iii_template.rs b/codes/rust/chapter_backtracking/preorder_traversal_iii_template.rs index 53271ef9a..6d168a0db 100644 --- a/codes/rust/chapter_backtracking/preorder_traversal_iii_template.rs +++ b/codes/rust/chapter_backtracking/preorder_traversal_iii_template.rs @@ -4,7 +4,7 @@ * Author: sjinzh (sjinzh@gmail.com) */ - include!("../include/include.rs"); +include!("../include/include.rs"); use std::{cell::RefCell, rc::Rc}; use tree_node::{vec_to_tree, TreeNode}; diff --git a/codes/rust/chapter_divide_and_conquer/hanota.rs b/codes/rust/chapter_divide_and_conquer/hanota.rs index 8cdb188c1..1c61e62a3 100644 --- a/codes/rust/chapter_divide_and_conquer/hanota.rs +++ b/codes/rust/chapter_divide_and_conquer/hanota.rs @@ -30,7 +30,7 @@ fn dfs(i: i32, src: &mut Vec, buf: &mut Vec, tar: &mut Vec) { } /* 求解汉诺塔 */ -fn hanota(A: &mut Vec, B: &mut Vec, C: &mut Vec) { +fn solve_hanota(A: &mut Vec, B: &mut Vec, C: &mut Vec) { let n = A.len() as i32; // 将 A 顶部 n 个圆盘借助 B 移到 C dfs(n, A, B, C); @@ -46,7 +46,7 @@ pub fn main() { println!("B = {:?}", B); println!("C = {:?}", C); - hanota(&mut A, &mut B, &mut C); + solve_hanota(&mut A, &mut B, &mut C); println!("圆盘移动完成后:"); println!("A = {:?}", A); diff --git a/codes/rust/chapter_dynamic_programming/min_cost_climbing_stairs_dp.rs b/codes/rust/chapter_dynamic_programming/min_cost_climbing_stairs_dp.rs index f204263d7..f3772d20a 100644 --- a/codes/rust/chapter_dynamic_programming/min_cost_climbing_stairs_dp.rs +++ b/codes/rust/chapter_dynamic_programming/min_cost_climbing_stairs_dp.rs @@ -4,7 +4,7 @@ * Author: sjinzh (sjinzh@gmail.com) */ - use std::cmp; +use std::cmp; /* 爬楼梯最小代价:动态规划 */ fn min_cost_climbing_stairs_dp(cost: &[i32]) -> i32 { diff --git a/codes/rust/chapter_searching/hashing_search.rs b/codes/rust/chapter_searching/hashing_search.rs index 6e6a1734f..f7898b4ee 100644 --- a/codes/rust/chapter_searching/hashing_search.rs +++ b/codes/rust/chapter_searching/hashing_search.rs @@ -4,12 +4,12 @@ * Author: sjinzh (sjinzh@gmail.com) */ - include!("../include/include.rs"); +include!("../include/include.rs"); - use std::collections::HashMap; - use std::rc::Rc; - use std::cell::RefCell; - use list_node::ListNode; +use std::collections::HashMap; +use std::rc::Rc; +use std::cell::RefCell; +use list_node::ListNode; /* 哈希查找(数组) */ fn hashing_search_array<'a>(map: &'a HashMap, target: i32) -> Option<&'a usize> { diff --git a/codes/rust/chapter_searching/linear_search.rs b/codes/rust/chapter_searching/linear_search.rs index 338975f6e..d5882391c 100644 --- a/codes/rust/chapter_searching/linear_search.rs +++ b/codes/rust/chapter_searching/linear_search.rs @@ -4,11 +4,11 @@ * Author: sjinzh (sjinzh@gmail.com) */ - include!("../include/include.rs"); +include!("../include/include.rs"); - use std::rc::Rc; - use std::cell::RefCell; - use list_node::ListNode; +use std::rc::Rc; +use std::cell::RefCell; +use list_node::ListNode; /* 线性查找(数组) */ fn linear_search_array(nums: &[i32], target: i32) -> i32 { diff --git a/codes/typescript/chapter_divide_and_conquer/hanota.ts b/codes/typescript/chapter_divide_and_conquer/hanota.ts index 8ee26b7d0..e5f0c55fe 100644 --- a/codes/typescript/chapter_divide_and_conquer/hanota.ts +++ b/codes/typescript/chapter_divide_and_conquer/hanota.ts @@ -28,7 +28,7 @@ function dfs(i: number, src: number[], buf: number[], tar: number[]): void { } /* 求解汉诺塔 */ -function hanota(A: number[], B: number[], C: number[]): void { +function solveHanota(A: number[], B: number[], C: number[]): void { const n = A.length; // 将 A 顶部 n 个圆盘借助 B 移到 C dfs(n, A, B, C); @@ -44,7 +44,7 @@ console.log(`A = ${JSON.stringify(A)}`); console.log(`B = ${JSON.stringify(B)}`); console.log(`C = ${JSON.stringify(C)}`); -hanota(A, B, C); +solveHanota(A, B, C); console.log('圆盘移动完成后:'); console.log(`A = ${JSON.stringify(A)}`); diff --git a/docs/chapter_divide_and_conquer/hanota_problem.md b/docs/chapter_divide_and_conquer/hanota_problem.md index 13bce32ac..4e129ff81 100644 --- a/docs/chapter_divide_and_conquer/hanota_problem.md +++ b/docs/chapter_divide_and_conquer/hanota_problem.md @@ -99,7 +99,7 @@ [class]{}-[func]{dfs} - [class]{}-[func]{hanota} + [class]{}-[func]{solveHanota} ``` === "Python" @@ -109,7 +109,7 @@ [class]{}-[func]{dfs} - [class]{}-[func]{hanota} + [class]{}-[func]{solve_hanota} ``` === "Go" @@ -119,7 +119,7 @@ [class]{}-[func]{dfsHanota} - [class]{}-[func]{hanota} + [class]{}-[func]{solveHanota} ``` === "JS" @@ -129,7 +129,7 @@ [class]{}-[func]{dfs} - [class]{}-[func]{hanota} + [class]{}-[func]{solveHanota} ``` === "TS" @@ -139,7 +139,7 @@ [class]{}-[func]{dfs} - [class]{}-[func]{hanota} + [class]{}-[func]{solveHanota} ``` === "C" @@ -149,7 +149,7 @@ [class]{}-[func]{dfs} - [class]{}-[func]{hanota} + [class]{}-[func]{solveHanota} ``` === "C#" @@ -169,7 +169,7 @@ [class]{}-[func]{dfs} - [class]{}-[func]{hanota} + [class]{}-[func]{solveHanota} ``` === "Zig" @@ -179,7 +179,7 @@ [class]{}-[func]{dfs} - [class]{}-[func]{hanota} + [class]{}-[func]{solveHanota} ``` === "Dart" @@ -189,7 +189,7 @@ [class]{}-[func]{dfs} - [class]{}-[func]{hanota} + [class]{}-[func]{solveHanota} ``` === "Rust" @@ -199,7 +199,7 @@ [class]{}-[func]{dfs} - [class]{}-[func]{hanota} + [class]{}-[func]{solve_hanota} ``` 如下图所示,汉诺塔问题形成一个高度为 $n$ 的递归树,每个节点代表一个子问题、对应一个开启的 `dfs()` 函数,**因此时间复杂度为 $O(2^n)$ ,空间复杂度为 $O(n)$** 。