Skip to content

Commit b9cb9d9

Browse files
committed
feat(soobing): week1 > two-sum
1 parent 22f71ca commit b9cb9d9

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

two-sum/soobing2.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function twoSum(nums: number[], target: number): number[] {
2+
const map = new Map();
3+
for (let i = 0; i < nums.length; i++) {
4+
map.set(nums[i], i);
5+
}
6+
7+
for (let i = 0; i < nums.length; i++) {
8+
const index = map.get(target - nums[i]);
9+
if (i === index || index === undefined) continue;
10+
11+
return [i, index];
12+
}
13+
14+
return [];
15+
}

0 commit comments

Comments
 (0)