Skip to content

Commit 6e87952

Browse files
committed
two sum solution
1 parent f291ae8 commit 6e87952

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

โ€Žtwo-sum/seungseung88.jsโ€Ž

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const twoSum = (nums, target) => {
2+
// ๋ฐฐ์—ด์„ ํ•œ ๋ฒˆ ์ˆœํšŒ
3+
for (let i = 0; i < nums.length; i += 1) {
4+
// target์ˆซ์ž์—์„œ ํ˜„์žฌ ์ˆซ์ž๋ฅผ ๋บ€ ์ˆซ์ž๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
5+
const targetIndex = nums.indexOf(target - nums[i]);
6+
// targetIndex๊ฐ€ ์กด์žฌํ•˜๊ณ (-1์ด ์•„๋‹˜), ํ˜„์žฌ ์ˆซ์ž์™€ ๊ฐ™์€ ์ธ๋ฑ์Šค ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ๋ฐ˜ํ™˜ํ•œ๋‹ค.
7+
if (targetIndex !== -1 && i !== targetIndex) {
8+
return [i, targetIndex];
9+
}
10+
}
11+
};
12+
13+
// ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n^2)
14+
// ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(1)

0 commit comments

Comments
ย (0)