Skip to content

Commit

Permalink
Fix two_sum.js and .ts
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Jun 16, 2023
1 parent 29e6617 commit d62156f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions codes/javascript/chapter_searching/two_sum.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
Expand Down
4 changes: 2 additions & 2 deletions codes/typescript/chapter_searching/two_sum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ function twoSumHashTable(nums: number[], target: number): number[] {
let m: Map<number, number> = 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 [];
Expand Down

0 comments on commit d62156f

Please sign in to comment.