Skip to content

Commit a6a8263

Browse files
committed
two-sum solution
1 parent 0e5ffe5 commit a6a8263

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

โ€Žtwo-sum/Yu-Won.jsโ€Ž

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* ๋ฌธ์ œ: https://leetcode.com/problems/two-sum/
3+
*
4+
* ์š”๊ตฌ์‚ฌํ•ญ:
5+
* nums: number[], target: number ๋ฅผ Input ์œผ๋กœ ๋ฐ›์•˜์„ ๋•Œ
6+
* nums์— ์žˆ๋Š” ๊ฐ’ ๋‘๊ฐœ๋ฅผ ๋”ํ–ˆ์„ ๋•Œ target ๊ณผ ์ •ํ™•ํ•˜๊ฒŒ ์ผ์น˜ํ•˜๊ฒŒ ๋˜๋Š” ๊ฐ’์„ ๋งŒ๋“ค๊ฒŒ ๋˜๋Š” ์ธ๋ฑ์Šค ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•œ๋‹ค.
7+
*
8+
* ํ•ด์‹œ๋งต ์ด์šฉ
9+
* */
10+
11+
const twoSum = (nums, target) => {
12+
const map = new Map();
13+
14+
for(let i = 0; i < nums.length; i++) {
15+
const result = target - nums[i];
16+
17+
// ์ด๋ฏธ ๊ฐ’์ด ์žˆ์—ˆ๋‹ค๋ฉด ๋ฆฌํ„ด
18+
if(map.has(result)) {
19+
return [map.get(result), i];
20+
}
21+
22+
// ํ˜„์žฌ ๊ฐ’์„ ์ €์žฅ
23+
map.set(nums[i], i);
24+
}
25+
}

0 commit comments

Comments
ย (0)