From 671e8f56bfc33fdf1c91802a362ce42cab2bd548 Mon Sep 17 00:00:00 2001 From: gyt95 Date: Thu, 15 Dec 2022 23:48:03 +0800 Subject: [PATCH] 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 []; }; ```