We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 90fec72 commit 89b7dd5Copy full SHA for 89b7dd5
two-sum/solbijae.ts
@@ -0,0 +1,19 @@
1
+function twoSum(nums: number[], target: number): number[] {
2
+ // 첫 코드: 시간 복잡도가 O(n^2)로 개선 필요
3
+ // for (let i=0; i<nums.length-1; i++) {
4
+ // for (let j=1; j<nums.length; j++) {
5
+ // if (nums[i] + nums[j] === target) return [i, j];
6
+ // }
7
8
+
9
+ // 시간 공간 모두 O(n)으로 만들기 위해 해시맵 사용
10
+ const map = new Map();
11
+ for (let i=0; i<nums.length; i++) {
12
+ const complement = target - nums[i];
13
+ if (map.get(complement) !== undefined) {
14
+ return [map.get(complement)!, i];
15
+ } else {
16
+ map.set(nums[i], i);
17
+ }
18
19
+};
0 commit comments