Skip to content

Commit 37ff84d

Browse files
committed
Add Two Sum
1 parent 12f1fcc commit 37ff84d

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function twoSum(nums: number[], target: number): number[] {
2+
const map = new Map<number, number>();
3+
for (let i = 0; i < nums.length; i++) {
4+
if (map.has(nums[i]!)) {
5+
return [i, map.get(nums[i]!)!];
6+
} else {
7+
map.set(target - nums[i]!, i);
8+
}
9+
}
10+
throw new Error('no answer');
11+
}

0 commit comments

Comments
 (0)