From 0a72d37578ccf799d6af4d7010b7f86bb7d7b69d Mon Sep 17 00:00:00 2001 From: Meng Du Date: Wed, 21 Dec 2022 18:57:28 +0800 Subject: [PATCH 1/4] create c folder and finished array --- codes/c/chapter_array_and_linkedlist/array.c | 124 +++++++++++++++++++ codes/c/include/PrintUtil.h | 30 +++++ codes/c/include/include.h | 11 ++ 3 files changed, 165 insertions(+) create mode 100644 codes/c/chapter_array_and_linkedlist/array.c create mode 100644 codes/c/include/PrintUtil.h create mode 100644 codes/c/include/include.h diff --git a/codes/c/chapter_array_and_linkedlist/array.c b/codes/c/chapter_array_and_linkedlist/array.c new file mode 100644 index 00000000..6217a566 --- /dev/null +++ b/codes/c/chapter_array_and_linkedlist/array.c @@ -0,0 +1,124 @@ +/* + * File: array.c + * Created Time: 2022-12-20 + * Author: MolDuM (moldum@163.com) + */ + +#include "../include/include.h" + +/* 随机返回一个数组元素 */ +int randomAccess(int* nums, int size) { + // 在区间 [0, size) 中随机抽取一个数字 + int randomIndex = rand() % size; + // 获取并返回随机元素 + int randomNum = nums[randomIndex]; + return randomNum; +} + +/* 扩展数组长度 */ +int* extend(int* nums, int size, int enlarge) { + // 初始化一个扩展长度后的数组 + int* res = (int *)malloc(sizeof(int) * (size + enlarge)); + // 将原数组中的所有元素复制到新数组 + for (int i = 0; i < size; i++) { + res[i] = nums[i]; + } + // 初始化扩展后的空间 + for (int i = size; i < size + enlarge; i++) { + res[i] = 0; + } + // 返回扩展后的新数组 + return res; +} + +/* 在数组的索引 index 处插入元素 num */ +void insert(int* nums, int size, int num, int index) { + // 把索引 index 以及之后的所有元素向后移动一位 + for (int i = size - 1; i > index; i--) { + nums[i] = nums[i - 1]; + } + // 将 num 赋给 index 处元素 + nums[index] = num; +} + +/* 删除索引 index 处元素 */ +void removeItem(int* nums, int size, int index) { + // 把索引 index 之后的所有元素向前移动一位 + for (int i = index; i < size - 1; i++) { + nums[i] = nums[i + 1]; + } +} + +/* 遍历数组 */ +void traverse(int* nums, int size) { + int count = 0; + // 通过索引遍历数组 + for (int i = 0; i < size; i++) { + count++; + } +} + +/* 在数组中查找指定元素 */ +int find(int* nums, int size, int target) { + for (int i = 0; i < size; i++) { + if (nums[i] == target) + return i; + } + return -1; +} + + +/* Driver Code */ +int main() { + /* 初始化数组 */ + int size = 5; + int arr[5]; + printf("数组 arr = "); + printArray(arr, size); + + int arr2[5] = { 1, 3, 2, 5, 4 }; + printf("数组 arr2 = "); + printArray(arr2, size); + + int* nums = (int *)malloc(sizeof(int) * size); + nums[0] = 1; + nums[1] = 3; + nums[2] = 2; + nums[3] = 5; + nums[4] = 4; + printf("数组 nums = "); + printArray(nums, size); + + /* 随机访问 */ + int randomNum = randomAccess(nums, size); + printf("在 nums 中获取随机元素 %d", randomNum); + + /* 长度扩展 */ + int enlarge = 3; + int* res = extend(nums, size, enlarge); + int* temp = nums; + nums = res; + free(temp); + size += enlarge; + printf("将数组长度扩展至 8 ,得到 nums = "); + printArray(nums, size); + + /* 插入元素 */ + insert(nums, size, 6, 3); + printf("在索引 3 处插入数字 6 ,得到 nums = "); + printArray(nums, size); + + /* 删除元素 */ + removeItem(nums, size, 2); + printf("删除索引 2 处的元素,得到 nums = "); + printArray(nums, size); + + /* 遍历数组 */ + traverse(nums, size); + + /* 查找元素 */ + int index = find(nums, size, 3); + printf("在 nums 中查找元素 3 ,得到索引 = %d\n", index); + + return 0; +} diff --git a/codes/c/include/PrintUtil.h b/codes/c/include/PrintUtil.h new file mode 100644 index 00000000..e471f038 --- /dev/null +++ b/codes/c/include/PrintUtil.h @@ -0,0 +1,30 @@ +/* + * File: PrintUtil.h + * Created Time: 2022-12-21 + * Author: MolDum (moldum@163.com) + */ + +#include +#include +#include + +// #include "ListNode.h" +// #include "TreeNode.h" + +/** + * @brief Print an Array + * + * @param arr + * @param n + */ + +static void printArray(int* arr, int n) +{ + printf("["); + for (int i = 0; i < n - 1; i++) { + printf("%d, ", arr[i]); + } + printf("%d]\n", arr[n-1]); +} + + diff --git a/codes/c/include/include.h b/codes/c/include/include.h new file mode 100644 index 00000000..aba6b492 --- /dev/null +++ b/codes/c/include/include.h @@ -0,0 +1,11 @@ +/* + * File: include.h + * Created Time: 2022-12-20 + * Author: MolDuM (moldum@163.com) + */ + +#include +#include +#include + +#include "PrintUtil.h" \ No newline at end of file From 2069dddd3b1c8a5271d269f853c1e3f44e153962 Mon Sep 17 00:00:00 2001 From: nuomi1 Date: Tue, 3 Jan 2023 22:24:50 +0800 Subject: [PATCH 2/4] feat: add Swift codes for space time tradeoff article --- codes/swift/Package.swift | 2 + .../leetcode_two_sum.swift | 46 +++++++++++++++++++ .../space_time_tradeoff.md | 33 +++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 codes/swift/chapter_computational_complexity/leetcode_two_sum.swift diff --git a/codes/swift/Package.swift b/codes/swift/Package.swift index 250c09af..a292d9fd 100644 --- a/codes/swift/Package.swift +++ b/codes/swift/Package.swift @@ -8,11 +8,13 @@ let package = Package( .executable(name: "time_complexity", targets: ["time_complexity"]), .executable(name: "worst_best_time_complexity", targets: ["worst_best_time_complexity"]), .executable(name: "space_complexity", targets: ["space_complexity"]), + .executable(name: "leetcode_two_sum", targets: ["leetcode_two_sum"]), ], targets: [ .target(name: "utils", path: "utils"), .executableTarget(name: "time_complexity", path: "chapter_computational_complexity", sources: ["time_complexity.swift"]), .executableTarget(name: "worst_best_time_complexity", path: "chapter_computational_complexity", sources: ["worst_best_time_complexity.swift"]), .executableTarget(name: "space_complexity", dependencies: ["utils"], path: "chapter_computational_complexity", sources: ["space_complexity.swift"]), + .executableTarget(name: "leetcode_two_sum", path: "chapter_computational_complexity", sources: ["leetcode_two_sum.swift"]), ] ) diff --git a/codes/swift/chapter_computational_complexity/leetcode_two_sum.swift b/codes/swift/chapter_computational_complexity/leetcode_two_sum.swift new file mode 100644 index 00000000..6ed0e580 --- /dev/null +++ b/codes/swift/chapter_computational_complexity/leetcode_two_sum.swift @@ -0,0 +1,46 @@ +/* + * File: leetcode_two_sum.swift + * Created Time: 2023-01-03 + * Author: nuomi1 (nuomi1@qq.com) + */ + +func twoSumBruteForce(nums: [Int], target: Int) -> [Int] { + // 两层循环,时间复杂度 O(n^2) + for i in nums.indices.dropLast() { + for j in nums.indices.dropFirst(i + 1) { + if nums[i] + nums[j] == target { + return [i, j] + } + } + } + return [0] +} + +func twoSumHashTable(nums: [Int], target: Int) -> [Int] { + // 辅助哈希表,空间复杂度 O(n) + var dic: [Int: Int] = [:] + // 单层循环,时间复杂度 O(n) + for i in nums.indices { + if let j = dic[target - nums[i]] { + return [j, i] + } + dic[nums[i]] = i + } + return [0] +} + +@main +enum LeetcodeTwoSum { + static func main() { + // ======= Test Case ======= + let nums = [2, 7, 11, 15] + let target = 9 + // ====== Driver Code ====== + // 方法一 + var res = twoSumBruteForce(nums: nums, target: target) + print("方法一 res = \(res)") + // 方法二 + res = twoSumHashTable(nums: nums, target: target) + print("方法二 res = \(res)") + } +} diff --git a/docs/chapter_computational_complexity/space_time_tradeoff.md b/docs/chapter_computational_complexity/space_time_tradeoff.md index 651aff14..0b86c11c 100644 --- a/docs/chapter_computational_complexity/space_time_tradeoff.md +++ b/docs/chapter_computational_complexity/space_time_tradeoff.md @@ -149,6 +149,22 @@ comments: true } ``` +=== "Swift" + + ```swift title="leetcode_two_sum.swift" + func twoSumBruteForce(nums: [Int], target: Int) -> [Int] { + // 两层循环,时间复杂度 O(n^2) + for i in nums.indices.dropLast() { + for j in nums.indices.dropFirst(i + 1) { + if nums[i] + nums[j] == target { + return [i, j] + } + } + } + return [0] + } + ``` + ### 方法二:辅助哈希表 时间复杂度 $O(N)$ ,空间复杂度 $O(N)$ ,属于「空间换时间」。 @@ -294,3 +310,20 @@ comments: true } } ``` + +=== "Swift" + + ```swift title="leetcode_two_sum.swift" + func twoSumHashTable(nums: [Int], target: Int) -> [Int] { + // 辅助哈希表,空间复杂度 O(n) + var dic: [Int: Int] = [:] + // 单层循环,时间复杂度 O(n) + for i in nums.indices { + if let j = dic[target - nums[i]] { + return [j, i] + } + dic[nums[i]] = i + } + return [0] + } + ``` From e0a3189f91c2af5c0d82d413ddd5c272594951a2 Mon Sep 17 00:00:00 2001 From: MolDuM <39506381+MolDuM@users.noreply.github.com> Date: Wed, 4 Jan 2023 14:56:16 +0800 Subject: [PATCH 3/4] Update array.c 3 changes: 1. In the enlarge part, I didn't change because I didn't find a good way to deal with the local variable and the clear definition. malloc is commonly used in LeetCode so I think it is not bad for a beginner. 2. I changed the initialization of the second array to make it in the same style as C++. 3. In the enlarge part in main, I deleted the code of pointer free to match the array operations. I also changed the operate array in the later part because the enlarged array cannot be assigned to the older array name. BTW, sorry for updating so late. Reading different version documents and book are really tiring and boring. --- codes/c/chapter_array_and_linkedlist/array.c | 30 ++++++-------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/codes/c/chapter_array_and_linkedlist/array.c b/codes/c/chapter_array_and_linkedlist/array.c index 6217a566..300124a7 100644 --- a/codes/c/chapter_array_and_linkedlist/array.c +++ b/codes/c/chapter_array_and_linkedlist/array.c @@ -76,16 +76,7 @@ int main() { printf("数组 arr = "); printArray(arr, size); - int arr2[5] = { 1, 3, 2, 5, 4 }; - printf("数组 arr2 = "); - printArray(arr2, size); - - int* nums = (int *)malloc(sizeof(int) * size); - nums[0] = 1; - nums[1] = 3; - nums[2] = 2; - nums[3] = 5; - nums[4] = 4; + int nums[5] = { 1, 3, 2, 5, 4 }; printf("数组 nums = "); printArray(nums, size); @@ -96,29 +87,26 @@ int main() { /* 长度扩展 */ int enlarge = 3; int* res = extend(nums, size, enlarge); - int* temp = nums; - nums = res; - free(temp); size += enlarge; printf("将数组长度扩展至 8 ,得到 nums = "); - printArray(nums, size); + printArray(res, size); /* 插入元素 */ - insert(nums, size, 6, 3); + insert(res, size, 6, 3); printf("在索引 3 处插入数字 6 ,得到 nums = "); - printArray(nums, size); + printArray(res, size); /* 删除元素 */ - removeItem(nums, size, 2); + removeItem(res, size, 2); printf("删除索引 2 处的元素,得到 nums = "); - printArray(nums, size); + printArray(res, size); /* 遍历数组 */ - traverse(nums, size); + traverse(res, size); /* 查找元素 */ - int index = find(nums, size, 3); - printf("在 nums 中查找元素 3 ,得到索引 = %d\n", index); + int index = find(res, size, 3); + printf("在 res 中查找元素 3 ,得到索引 = %d\n", index); return 0; } From 57757943511c9fc4577bfd0aa72298dc1603fe85 Mon Sep 17 00:00:00 2001 From: moonache <476681765@qq.com> Date: Wed, 4 Jan 2023 19:27:35 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E9=94=99=E5=88=AB=E5=AD=97=EF=BC=9A?= =?UTF-8?q?=E4=BB=8E=E9=A1=B6=E7=BD=AE=E5=BA=95=20->=20=E4=BB=8E=E9=A1=B6?= =?UTF-8?q?=E8=87=B3=E5=BA=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/chapter_tree/binary_tree.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/chapter_tree/binary_tree.md b/docs/chapter_tree/binary_tree.md index 0c858fd3..3e322222 100644 --- a/docs/chapter_tree/binary_tree.md +++ b/docs/chapter_tree/binary_tree.md @@ -120,7 +120,7 @@ comments: true - 「根结点 Root Node」:二叉树最顶层的结点,其没有父结点; - 「叶结点 Leaf Node」:没有子结点的结点,其两个指针都指向 $\text{null}$ ; -- 结点所处「层 Level」:从顶置底依次增加,根结点所处层为 1 ; +- 结点所处「层 Level」:从顶至底依次增加,根结点所处层为 1 ; - 结点「度 Degree」:结点的子结点数量。二叉树中,度的范围是 0, 1, 2 ; - 「边 Edge」:连接两个结点的边,即结点指针; - 二叉树「高度」:二叉树中根结点到最远叶结点走过边的数量;