From 3a01f21dcaf53529b3e7592309c75c72d7934782 Mon Sep 17 00:00:00 2001 From: gyt95 Date: Thu, 15 Dec 2022 11:06:51 +0800 Subject: [PATCH 01/10] Add JS for chapter of computational complexity --- .../leetcode_two_sum.js | 36 +++++++++++++++++++ .../space_time_tradeoff.md | 25 +++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 codes/javascript/chapter_computational_complexity/leetcode_two_sum.js diff --git a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js new file mode 100644 index 000000000..432e2b2ee --- /dev/null +++ b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js @@ -0,0 +1,36 @@ +/* + * @Author: gyt95 (gytkwan@gmail.com) + * @Date: 2022-12-15 10:51:54 + * @Last Modified by: gyt95 (gytkwan@gmail.com) + * @Last Modified time: 2022-12-15 10:56:26 + */ + +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +function twoSumBruteForce(nums, target) { + 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] + } + } + } +} + +function twoSumHashTable(nums, target) { + // 辅助哈希表,空间复杂度 O(n) + let m = {} + // 单层循环,时间复杂度 O(n) + for (let i = 0; i < nums.length; i++) { + if (m[nums[i]] !== undefined) { + return [m[nums[i]], i] + } else { + m[target - nums[i]] = i; + } + } +} \ 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..8cbb3b5b2 100644 --- a/docs/chapter_computational_complexity/space_time_tradeoff.md +++ b/docs/chapter_computational_complexity/space_time_tradeoff.md @@ -90,7 +90,17 @@ comments: true === "JavaScript" ```js title="leetcode_two_sum.js" - + function twoSumBruteForce(nums, target) { + 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] + } + } + } + } ``` === "TypeScript" @@ -193,7 +203,18 @@ comments: true === "JavaScript" ```js title="leetcode_two_sum.js" - + function twoSumHashTable(nums, target) { + // 辅助哈希表,空间复杂度 O(n) + let m = {} + // 单层循环,时间复杂度 O(n) + for (let i = 0; i < nums.length; i++) { + if (m[nums[i]] !== undefined) { + return [m[nums[i]], i] + } else { + m[target - nums[i]] = i; + } + } + } ``` === "TypeScript" From 5694c8e8ff684475d6c8b97accc24ff89091f15f Mon Sep 17 00:00:00 2001 From: gyt95 Date: Thu, 15 Dec 2022 23:41:06 +0800 Subject: [PATCH 02/10] update code style for js --- .../leetcode_two_sum.js | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js index 432e2b2ee..7629dd3c4 100644 --- a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js +++ b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js @@ -2,7 +2,7 @@ * @Author: gyt95 (gytkwan@gmail.com) * @Date: 2022-12-15 10:51:54 * @Last Modified by: gyt95 (gytkwan@gmail.com) - * @Last Modified time: 2022-12-15 10:56:26 + * @Last Modified time: 2022-12-15 23:40:09 */ /** @@ -11,26 +11,26 @@ * @return {number[]} */ function twoSumBruteForce(nums, target) { - 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]; + } + } } - } } function twoSumHashTable(nums, target) { - // 辅助哈希表,空间复杂度 O(n) - let m = {} - // 单层循环,时间复杂度 O(n) - for (let i = 0; i < nums.length; i++) { - if (m[nums[i]] !== undefined) { - return [m[nums[i]], i] - } else { - m[target - nums[i]] = i; + // 辅助哈希表,空间复杂度 O(n) + let m = {}; + // 单层循环,时间复杂度 O(n) + for (let i = 0; i < nums.length; i++) { + if (m[nums[i]] !== undefined) { + return [m[nums[i]], i]; + } else { + m[target - nums[i]] = i; + } } - } } \ No newline at end of file From 94d5de6096c25a75a7810e3a42b741d7ae753512 Mon Sep 17 00:00:00 2001 From: gyt95 Date: Thu, 15 Dec 2022 23:44:26 +0800 Subject: [PATCH 03/10] Update js code style in space_time_tradeoff.md --- .../space_time_tradeoff.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/chapter_computational_complexity/space_time_tradeoff.md b/docs/chapter_computational_complexity/space_time_tradeoff.md index 8cbb3b5b2..140f1ef95 100644 --- a/docs/chapter_computational_complexity/space_time_tradeoff.md +++ b/docs/chapter_computational_complexity/space_time_tradeoff.md @@ -204,16 +204,16 @@ comments: true ```js title="leetcode_two_sum.js" function twoSumHashTable(nums, target) { - // 辅助哈希表,空间复杂度 O(n) - let m = {} - // 单层循环,时间复杂度 O(n) - for (let i = 0; i < nums.length; i++) { - if (m[nums[i]] !== undefined) { - return [m[nums[i]], i] - } else { - m[target - nums[i]] = i; + // 辅助哈希表,空间复杂度 O(n) + let m = {} + // 单层循环,时间复杂度 O(n) + for (let i = 0; i < nums.length; i++) { + if (m[nums[i]] !== undefined) { + return [m[nums[i]], i] + } else { + m[target - nums[i]] = i; + } } - } } ``` From 76693ab0fbe7e0751064cbacd988e7b6a13fa230 Mon Sep 17 00:00:00 2001 From: gyt95 Date: Thu, 15 Dec 2022 23:50:08 +0800 Subject: [PATCH 04/10] Update js code format --- .../space_time_tradeoff.md | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/chapter_computational_complexity/space_time_tradeoff.md b/docs/chapter_computational_complexity/space_time_tradeoff.md index 140f1ef95..99a08d95e 100644 --- a/docs/chapter_computational_complexity/space_time_tradeoff.md +++ b/docs/chapter_computational_complexity/space_time_tradeoff.md @@ -91,15 +91,15 @@ comments: true ```js title="leetcode_two_sum.js" function twoSumBruteForce(nums, target) { - 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]; + } + } } - } } ``` @@ -205,11 +205,11 @@ comments: true ```js title="leetcode_two_sum.js" function twoSumHashTable(nums, target) { // 辅助哈希表,空间复杂度 O(n) - let m = {} + let m = {}; // 单层循环,时间复杂度 O(n) for (let i = 0; i < nums.length; i++) { if (m[nums[i]] !== undefined) { - return [m[nums[i]], i] + return [m[nums[i]], i]; } else { m[target - nums[i]] = i; } From f7952fca2585963653bc20c47175f6ac822bea03 Mon Sep 17 00:00:00 2001 From: gyt95 Date: Fri, 16 Dec 2022 00:01:47 +0800 Subject: [PATCH 05/10] Add a test --- .../leetcode_two_sum.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js index 7629dd3c4..4fe58a433 100644 --- a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js +++ b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js @@ -2,7 +2,7 @@ * @Author: gyt95 (gytkwan@gmail.com) * @Date: 2022-12-15 10:51:54 * @Last Modified by: gyt95 (gytkwan@gmail.com) - * @Last Modified time: 2022-12-15 23:40:09 + * @Last Modified time: 2022-12-16 00:00:35 */ /** @@ -33,4 +33,13 @@ function twoSumHashTable(nums, target) { m[target - nums[i]] = i; } } -} \ No newline at end of file +} + +/* Driver Code */ +let nums = [2, 7, 11, 15] +twoSumBruteForce(nums) +console.log("使用暴力枚举后得到结果:", nums) + +let nums1 = [2, 7, 11, 15] +twoSumHashTable(nums1) +console.log("使用辅助哈希表后得到结果", nums1) From b34fa3b1b1a78368f811ed24f95eb910699e1886 Mon Sep 17 00:00:00 2001 From: gyt95 Date: Fri, 16 Dec 2022 00:04:05 +0800 Subject: [PATCH 06/10] Update the file header following other js file --- .../chapter_computational_complexity/leetcode_two_sum.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js index 4fe58a433..3ced0d492 100644 --- a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js +++ b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js @@ -1,8 +1,7 @@ -/* - * @Author: gyt95 (gytkwan@gmail.com) - * @Date: 2022-12-15 10:51:54 - * @Last Modified by: gyt95 (gytkwan@gmail.com) - * @Last Modified time: 2022-12-16 00:00:35 +/** + * File: leetcode_two_sum.js + * Created Time: 2022-12-15 + * Author: gyt95 (gytkwan@gmail.com) */ /** From 87b6026529711ccd1e51c0a13988f606f43ad3ff Mon Sep 17 00:00:00 2001 From: gyt95 Date: Fri, 16 Dec 2022 00:12:31 +0800 Subject: [PATCH 07/10] Update the test --- .../chapter_computational_complexity/leetcode_two_sum.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js index 3ced0d492..24b5ba84d 100644 --- a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js +++ b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js @@ -35,10 +35,10 @@ function twoSumHashTable(nums, target) { } /* Driver Code */ -let nums = [2, 7, 11, 15] -twoSumBruteForce(nums) +let nums = [2, 7, 11, 15], target = 9; +twoSumBruteForce(nums, target) console.log("使用暴力枚举后得到结果:", nums) -let nums1 = [2, 7, 11, 15] -twoSumHashTable(nums1) +let nums1 = [2, 7, 11, 15], target1 = 9; +twoSumHashTable(nums1, target1) console.log("使用辅助哈希表后得到结果", nums1) From c8da48c0d67ecf631d135c85344cda4eac73ec92 Mon Sep 17 00:00:00 2001 From: gyt95 Date: Fri, 16 Dec 2022 14:38:44 +0800 Subject: [PATCH 08/10] Remove function comment --- .../chapter_computational_complexity/leetcode_two_sum.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js index 24b5ba84d..8afe1fd4d 100644 --- a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js +++ b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js @@ -4,11 +4,6 @@ * Author: gyt95 (gytkwan@gmail.com) */ -/** - * @param {number[]} nums - * @param {number} target - * @return {number[]} - */ function twoSumBruteForce(nums, target) { let n = nums.length; // 两层循环,时间复杂度 O(n^2) From dc985cb962440a2c1b81d466e17bda112ac828c9 Mon Sep 17 00:00:00 2001 From: gyt95 Date: Fri, 16 Dec 2022 16:27:13 +0800 Subject: [PATCH 09/10] Update js code and docs --- .../leetcode_two_sum.js | 17 ++++++++++------- .../space_time_tradeoff.md | 4 +++- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js index 8afe1fd4d..2166a29b8 100644 --- a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js +++ b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js @@ -5,7 +5,7 @@ */ function twoSumBruteForce(nums, target) { - 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++) { @@ -14,6 +14,7 @@ function twoSumBruteForce(nums, target) { } } } + return []; } function twoSumHashTable(nums, target) { @@ -27,13 +28,15 @@ function twoSumHashTable(nums, target) { m[target - nums[i]] = i; } } + return []; } /* 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 res = twoSumBruteForce(nums, target) +console.log("方法一 res = ", res) -let nums1 = [2, 7, 11, 15], target1 = 9; -twoSumHashTable(nums1, target1) -console.log("使用辅助哈希表后得到结果", nums1) +// 方法二 +res = twoSumHashTable(nums, target) +console.log("方法二 res = ", res) diff --git a/docs/chapter_computational_complexity/space_time_tradeoff.md b/docs/chapter_computational_complexity/space_time_tradeoff.md index 99a08d95e..9ebfea7b4 100644 --- a/docs/chapter_computational_complexity/space_time_tradeoff.md +++ b/docs/chapter_computational_complexity/space_time_tradeoff.md @@ -91,7 +91,7 @@ comments: true ```js title="leetcode_two_sum.js" function twoSumBruteForce(nums, target) { - 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++) { @@ -100,6 +100,7 @@ comments: true } } } + return []; } ``` @@ -214,6 +215,7 @@ comments: true m[target - nums[i]] = i; } } + return []; } ``` From 19d7356e8fba39187fb938b3042fdb32ace42a3f Mon Sep 17 00:00:00 2001 From: gyt95 Date: Fri, 16 Dec 2022 16:34:30 +0800 Subject: [PATCH 10/10] Add semicolon in test --- .../chapter_computational_complexity/leetcode_two_sum.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js index 2166a29b8..0db9cd705 100644 --- a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js +++ b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js @@ -34,9 +34,10 @@ function twoSumHashTable(nums, target) { /* Driver Code */ // 方法一 const nums = [2, 7, 11, 15], target = 9; -let res = twoSumBruteForce(nums, target) -console.log("方法一 res = ", res) + +let res = twoSumBruteForce(nums, target); +console.log("方法一 res = ", res); // 方法二 -res = twoSumHashTable(nums, target) -console.log("方法二 res = ", res) +res = twoSumHashTable(nums, target); +console.log("方法二 res = ", res);