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 []; }; ```