Skip to content

Commit f142d9d

Browse files
committed
two-sum solution
1 parent aaf8791 commit f142d9d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

two-sum/seona926.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
let twoSum = function (nums, target) {
7+
let indices = {};
8+
9+
nums.forEach((item, index) => {
10+
indices[item] = index;
11+
});
12+
13+
for (let i = 0; i < nums.length; i++) {
14+
let findNum = target - nums[i];
15+
16+
if (indices[findNum] !== i && findNum.toString() in indices) {
17+
return [indices[findNum], i];
18+
}
19+
}
20+
};
21+
22+
/*
23+
1. 시간복잡도: O(n)
24+
- forEach와 for루프의 시간복잡도가 각 O(n)
25+
2. 공간복잡도: O(n)
26+
- indices 객체의 공간복잡도가 O(n), 나머지는 O(1)
27+
*/

0 commit comments

Comments
 (0)