Skip to content

Commit d754bb7

Browse files
authored
Merge pull request #799 from nakjun12/main
[nakjun12] Week 3
2 parents 4cf7b2c + 00b6cde commit d754bb7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

two-sum/nakjun12.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* TC: O(n)
3+
* SC: O(n)
4+
* */
5+
function twoSum(nums: number[], target: number): number[] {
6+
const indices = {};
7+
8+
for (let i = 0; i < nums.length; i++) {
9+
const curNum = nums[i];
10+
const complement = target - curNum;
11+
12+
if (complement in indices) {
13+
return [indices[complement], i];
14+
}
15+
16+
indices[curNum] = i;
17+
}
18+
19+
return [];
20+
}

0 commit comments

Comments
 (0)