Skip to content

Commit e39c2e5

Browse files
committed
Add week 3 solutions: two-sum
1 parent daa942f commit e39c2e5

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

two-sum/gitsunmin.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* https://leetcode.com/problems/two-sum
3+
* time complexity : O(n)
4+
* space complexity : O(n)
5+
*/
6+
function twoSum(nums: number[], target: number): number[] {
7+
const m = new Map<number, number>();
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
if (m.has(nums[i]))
11+
return [m.get(nums[i]), i];
12+
m.set(target - nums[i], i);
13+
}
14+
15+
return [-1, -1];
16+
};

0 commit comments

Comments
 (0)