From d62156fb58fb9ea69fa60177c430f558d494a5c5 Mon Sep 17 00:00:00 2001 From: krahets Date: Fri, 16 Jun 2023 21:28:48 +0800 Subject: [PATCH] Fix two_sum.js and .ts --- codes/javascript/chapter_searching/two_sum.js | 6 +++--- codes/typescript/chapter_searching/two_sum.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/codes/javascript/chapter_searching/two_sum.js b/codes/javascript/chapter_searching/two_sum.js index ae7757672..4e47a41e9 100644 --- a/codes/javascript/chapter_searching/two_sum.js +++ b/codes/javascript/chapter_searching/two_sum.js @@ -24,10 +24,10 @@ function twoSumHashTable(nums, target) { let m = {}; // 单层循环,时间复杂度 O(n) for (let i = 0; i < nums.length; i++) { - if (m[nums[i]] !== undefined) { - return [m[nums[i]], i]; + if (m[target - nums[i]] !== undefined) { + return [m[target-nums[i]], i]; } else { - m[target - nums[i]] = i; + m[nums[i]] = i; } } return []; diff --git a/codes/typescript/chapter_searching/two_sum.ts b/codes/typescript/chapter_searching/two_sum.ts index 51bd2af57..128413d8f 100644 --- a/codes/typescript/chapter_searching/two_sum.ts +++ b/codes/typescript/chapter_searching/two_sum.ts @@ -24,11 +24,11 @@ function twoSumHashTable(nums: number[], target: number): number[] { let m: Map = new Map(); // 单层循环,时间复杂度 O(n) for (let i = 0; i < nums.length; i++) { - let index = m.get(nums[i]); + let index = m.get(target - nums[i]); if (index !== undefined) { return [index, i]; } else { - m.set(target - nums[i], i); + m.set(nums[i], i); } } return [];