From 4a31f909c6753ae9403306b9f20ca5f5cae4fde0 Mon Sep 17 00:00:00 2001 From: gyt95 Date: Fri, 16 Dec 2022 00:11:06 +0800 Subject: [PATCH] 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 []; }; ```