Skip to content

Commit 3856037

Browse files
feat: two-sum solution
1 parent 7f4c264 commit 3856037

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* ์ •์ˆ˜ ์ˆซ์ž ๋ฐฐ์—ด๊ณผ ์ •์ˆ˜ target
3+
* ์ˆซ์ž ํ•ฉ์ด target๊ณผ ๊ฐ™์€ ๋‘ ์ˆซ์ž์˜ index๋ฅผ ๋ฆฌํ„ด.
4+
* ๊ฐ™์€ ์š”์†Œ ๋‘๋ฒˆ X. ๋‹ต์€ ํ•ญ์ƒ 1๊ฐœ
5+
* ์ •๋ ฌํ•„์š” X
6+
*/
7+
8+
/**
9+
* @param {number[]} nums
10+
* @param {number} target
11+
* @return {number[]}
12+
*/
13+
var twoSum = function (nums, target) {
14+
//์ˆœํšŒ. target์—์„œ nums[i]๋ฅผ ๋บ€ ์š”์†Œ๋ฅผ ์ฐพ๊ธฐ.
15+
//2์ค‘ํฌ๋ฌธ. ์‹œ๊ฐ„๋ณต์žก๋„ O(1)~O(N^2)
16+
for (let i = 0; i < nums.length; i++) {
17+
const subNum = target - nums[i]; // ๊ณต๊ฐ„ O(1)
18+
for (let j = i + 1; j < nums.length; j++) {
19+
if (nums[j] == subNum) {
20+
return [i, j];
21+
}
22+
}
23+
}
24+
};

0 commit comments

Comments
ย (0)