Skip to content

Commit 6b3390e

Browse files
committed
feat: two sum 풀이
1 parent a376b55 commit 6b3390e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

two-sum/grapefruit13.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @description nums 배열에서 두 수를 더해 target을 만족하는 인덱스를 반환
3+
* @param {number[]} nums - 숫자 배열
4+
* @param {number} target - 목표 숫자
5+
* @return {number[]} - 두 수의 인덱스
6+
*/
7+
var twoSum = function (nums, target) {
8+
const map = new Map();
9+
10+
for (let i = 0; i < nums.length; i++) {
11+
const current = nums[i];
12+
const needed = target - current;
13+
if (map.has(needed)) {
14+
return [map.get(needed), i];
15+
}
16+
map.set(current, i);
17+
}
18+
};

0 commit comments

Comments
 (0)