From dbb663703c17155fa304095b91abde5a2c450216 Mon Sep 17 00:00:00 2001 From: gyt95 Date: Thu, 15 Dec 2022 11:42:38 +0800 Subject: [PATCH 1/6] Add TS for chapter of computational complexity --- .../leetcode_two_sum.ts | 34 +++++++++++++++++++ .../space_time_tradeoff.md | 28 +++++++++++++-- 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts diff --git a/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts b/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts new file mode 100644 index 000000000..f0aaa17b7 --- /dev/null +++ b/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts @@ -0,0 +1,34 @@ +/* + * @Author: gyt95 (gytkwan@gmail.com) + * @Date: 2022-12-15 11:26:38 + * @Last Modified by: gyt95 (gytkwan@gmail.com) + * @Last Modified time: 2022-12-15 11:38:22 + */ + +function twoSumBruteForce(nums: number[], target: number): number[] { + let n = nums.length; + // 两层循环,时间复杂度 O(n^2) + for (let i = 0; i < n; i++) { + for (let j = i + 1; j < n; j++) { + if (nums[i] + nums[j] === target) { + return [i, j] + } + } + } + return []; +}; + +function twoSumHashTable(nums: number[], target: number): number[] { + // 辅助哈希表,空间复杂度 O(n) + let m: Map = new Map() + // 单层循环,时间复杂度 O(n) + for (let i = 0; i < nums.length; i++) { + let index = m.get(nums[i]) + if (index !== undefined) { + return [index, i] + } else { + m.set(target - nums[i], i) + } + } + return []; +}; \ No newline at end of file diff --git a/docs/chapter_computational_complexity/space_time_tradeoff.md b/docs/chapter_computational_complexity/space_time_tradeoff.md index ca54e907f..0613c4a07 100644 --- a/docs/chapter_computational_complexity/space_time_tradeoff.md +++ b/docs/chapter_computational_complexity/space_time_tradeoff.md @@ -96,7 +96,18 @@ comments: true === "TypeScript" ```typescript title="leetcode_two_sum.ts" - + function twoSumBruteForce(nums: number[], target: number): number[] { + let n = nums.length; + // 两层循环,时间复杂度 O(n^2) + for (let i = 0; i < n; i++) { + for (let j = i + 1; j < n; j++) { + if (nums[i] + nums[j] === target) { + return [i, j] + } + } + } + return []; + }; ``` === "C" @@ -199,7 +210,20 @@ comments: true === "TypeScript" ```typescript title="leetcode_two_sum.ts" - + function twoSumHashTable(nums: number[], target: number): number[] { + // 辅助哈希表,空间复杂度 O(n) + let m: Map = new Map() + // 单层循环,时间复杂度 O(n) + for (let i = 0; i < nums.length; i++) { + let index = m.get(nums[i]) + if (index !== undefined) { + return [index, i] + } else { + m.set(target - nums[i], i) + } + } + return []; + }; ``` === "C" From 671e8f56bfc33fdf1c91802a362ce42cab2bd548 Mon Sep 17 00:00:00 2001 From: gyt95 Date: Thu, 15 Dec 2022 23:48:03 +0800 Subject: [PATCH 2/6] Update ts code style --- .../leetcode_two_sum.ts | 42 +++++++++---------- .../space_time_tradeoff.md | 22 +++++----- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts b/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts index f0aaa17b7..6e9300606 100644 --- a/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts +++ b/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts @@ -2,33 +2,33 @@ * @Author: gyt95 (gytkwan@gmail.com) * @Date: 2022-12-15 11:26:38 * @Last Modified by: gyt95 (gytkwan@gmail.com) - * @Last Modified time: 2022-12-15 11:38:22 + * @Last Modified time: 2022-12-15 23:46:25 */ function twoSumBruteForce(nums: number[], target: number): number[] { - let n = nums.length; - // 两层循环,时间复杂度 O(n^2) - for (let i = 0; i < n; i++) { - for (let j = i + 1; j < n; j++) { - if (nums[i] + nums[j] === target) { - return [i, j] - } + let n = nums.length; + // 两层循环,时间复杂度 O(n^2) + for (let i = 0; i < n; i++) { + for (let j = i + 1; j < n; j++) { + if (nums[i] + nums[j] === target) { + return [i, j]; + } + } } - } - return []; + return []; }; function twoSumHashTable(nums: number[], target: number): number[] { - // 辅助哈希表,空间复杂度 O(n) - let m: Map = new Map() - // 单层循环,时间复杂度 O(n) - for (let i = 0; i < nums.length; i++) { - let index = m.get(nums[i]) - if (index !== undefined) { - return [index, i] - } else { - m.set(target - nums[i], i) + // 辅助哈希表,空间复杂度 O(n) + let m: Map = new Map(); + // 单层循环,时间复杂度 O(n) + for (let i = 0; i < nums.length; i++) { + let index = m.get(nums[i]); + if (index !== undefined) { + return [index, i]; + } else { + m.set(target - nums[i], i); + } } - } - return []; + return []; }; \ No newline at end of file diff --git a/docs/chapter_computational_complexity/space_time_tradeoff.md b/docs/chapter_computational_complexity/space_time_tradeoff.md index 0613c4a07..362fe3e57 100644 --- a/docs/chapter_computational_complexity/space_time_tradeoff.md +++ b/docs/chapter_computational_complexity/space_time_tradeoff.md @@ -211,18 +211,18 @@ comments: true ```typescript title="leetcode_two_sum.ts" function twoSumHashTable(nums: number[], target: number): number[] { - // 辅助哈希表,空间复杂度 O(n) - let m: Map = new Map() - // 单层循环,时间复杂度 O(n) - for (let i = 0; i < nums.length; i++) { - let index = m.get(nums[i]) - if (index !== undefined) { - return [index, i] - } else { - m.set(target - nums[i], i) + // 辅助哈希表,空间复杂度 O(n) + let m: Map = new Map(); + // 单层循环,时间复杂度 O(n) + for (let i = 0; i < nums.length; i++) { + let index = m.get(nums[i]); + if (index !== undefined) { + return [index, i]; + } else { + m.set(target - nums[i], i); + } } - } - return []; + return []; }; ``` From 3265e3fde03264910be94f1d6a1f7794211b9d69 Mon Sep 17 00:00:00 2001 From: gyt95 Date: Fri, 16 Dec 2022 00:07:02 +0800 Subject: [PATCH 3/6] Update the file header --- .../chapter_computational_complexity/leetcode_two_sum.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts b/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts index 6e9300606..e3ff51539 100644 --- a/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts +++ b/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts @@ -1,8 +1,7 @@ /* - * @Author: gyt95 (gytkwan@gmail.com) - * @Date: 2022-12-15 11:26:38 - * @Last Modified by: gyt95 (gytkwan@gmail.com) - * @Last Modified time: 2022-12-15 23:46:25 + * File: leetcode_two_sum.ts + * Created Time: 2022-12-15 + * Author: gyt95 (gytkwan@gmail.com) */ function twoSumBruteForce(nums: number[], target: number): number[] { From 4a31f909c6753ae9403306b9f20ca5f5cae4fde0 Mon Sep 17 00:00:00 2001 From: gyt95 Date: Fri, 16 Dec 2022 00:11:06 +0800 Subject: [PATCH 4/6] Add a test and update ts code style --- .../leetcode_two_sum.ts | 11 ++++++++++- .../space_time_tradeoff.md | 18 +++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts b/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts index e3ff51539..865045c79 100644 --- a/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts +++ b/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts @@ -30,4 +30,13 @@ function twoSumHashTable(nums: number[], target: number): number[] { } } return []; -}; \ No newline at end of file +}; + +/* Driver Code */ +let nums = [2, 7, 11, 15] +twoSumBruteForce(nums, 9) +console.log("使用暴力枚举后得到结果:", nums) + +let nums1 = [2, 7, 11, 15] +twoSumHashTable(nums1, 9) +console.log("使用辅助哈希表后得到结果", nums1) \ No newline at end of file diff --git a/docs/chapter_computational_complexity/space_time_tradeoff.md b/docs/chapter_computational_complexity/space_time_tradeoff.md index 362fe3e57..40b1d57e9 100644 --- a/docs/chapter_computational_complexity/space_time_tradeoff.md +++ b/docs/chapter_computational_complexity/space_time_tradeoff.md @@ -97,16 +97,16 @@ comments: true ```typescript title="leetcode_two_sum.ts" function twoSumBruteForce(nums: number[], target: number): number[] { - let n = nums.length; - // 两层循环,时间复杂度 O(n^2) - for (let i = 0; i < n; i++) { - for (let j = i + 1; j < n; j++) { - if (nums[i] + nums[j] === target) { - return [i, j] - } + let n = nums.length; + // 两层循环,时间复杂度 O(n^2) + for (let i = 0; i < n; i++) { + for (let j = i + 1; j < n; j++) { + if (nums[i] + nums[j] === target) { + return [i, j]; + } + } } - } - return []; + return []; }; ``` From 45aca03b92f3a766735972e6d4d3a886ee9c639d Mon Sep 17 00:00:00 2001 From: gyt95 Date: Fri, 16 Dec 2022 00:13:40 +0800 Subject: [PATCH 5/6] Update the test --- .../chapter_computational_complexity/leetcode_two_sum.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts b/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts index 865045c79..0087d1ce8 100644 --- a/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts +++ b/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts @@ -33,10 +33,10 @@ function twoSumHashTable(nums: number[], target: number): number[] { }; /* Driver Code */ -let nums = [2, 7, 11, 15] -twoSumBruteForce(nums, 9) +let nums = [2, 7, 11, 15], target = 9; +twoSumBruteForce(nums, target) console.log("使用暴力枚举后得到结果:", nums) -let nums1 = [2, 7, 11, 15] -twoSumHashTable(nums1, 9) +let nums1 = [2, 7, 11, 15], target1 = 9; +twoSumHashTable(nums1, target1) console.log("使用辅助哈希表后得到结果", nums1) \ No newline at end of file From 84caa60cd4f0fc2199a995cd75ac0b3b7b2b0776 Mon Sep 17 00:00:00 2001 From: gyt95 Date: Fri, 16 Dec 2022 16:33:12 +0800 Subject: [PATCH 6/6] Update ts code and docs --- .../leetcode_two_sum.ts | 16 +++++++++------- .../space_time_tradeoff.md | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts b/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts index 0087d1ce8..8e3c4cd76 100644 --- a/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts +++ b/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts @@ -5,7 +5,7 @@ */ function twoSumBruteForce(nums: number[], target: number): number[] { - let n = nums.length; + const n = nums.length; // 两层循环,时间复杂度 O(n^2) for (let i = 0; i < n; i++) { for (let j = i + 1; j < n; j++) { @@ -33,10 +33,12 @@ function twoSumHashTable(nums: number[], target: number): number[] { }; /* Driver Code */ -let nums = [2, 7, 11, 15], target = 9; -twoSumBruteForce(nums, target) -console.log("使用暴力枚举后得到结果:", nums) +// 方法一 +const nums = [2, 7, 11, 15], target = 9; -let nums1 = [2, 7, 11, 15], target1 = 9; -twoSumHashTable(nums1, target1) -console.log("使用辅助哈希表后得到结果", nums1) \ No newline at end of file +let res = twoSumBruteForce(nums, target); +console.log("方法一 res = ", res); + +// 方法二 +res = twoSumHashTable(nums, target); +console.log("方法二 res = ", res); \ No newline at end of file diff --git a/docs/chapter_computational_complexity/space_time_tradeoff.md b/docs/chapter_computational_complexity/space_time_tradeoff.md index 40b1d57e9..b29b03fa0 100644 --- a/docs/chapter_computational_complexity/space_time_tradeoff.md +++ b/docs/chapter_computational_complexity/space_time_tradeoff.md @@ -97,7 +97,7 @@ comments: true ```typescript title="leetcode_two_sum.ts" function twoSumBruteForce(nums: number[], target: number): number[] { - let n = nums.length; + const n = nums.length; // 两层循环,时间复杂度 O(n^2) for (let i = 0; i < n; i++) { for (let j = i + 1; j < n; j++) {