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)