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 f6a648d commit 7bd21e1Copy full SHA for 7bd21e1
two-sum/juhui-jeong.ts
@@ -0,0 +1,41 @@
1
+/*
2
+시간 복잡도: O(n)
3
+공간 복잡도: O(n)
4
+*.
5
+function twoSum(nums: number[], target: number): number[] {
6
+ const map = new Map<number, number>();
7
+
8
+ for (let i = 0; i < nums.length; i++) {
9
+ const num = nums[i];
10
+ const diff = target - num;
11
12
+ if (map.has(diff)) {
13
+ return [map.get(diff)!, i];
14
+ }
15
16
+ map.set(num, i);
17
18
19
+ return [];
20
+}
21
22
23
+이전 풀이
24
+시간 복잡도: O(n²)
25
+공간 복잡도: O(1)
26
27
28
+ for (let j = 0; j < nums.length; j++) {
29
+ // 동일한 index일 때는 안됨.
30
+ if (i === j) {
31
+ continue;
32
33
+ if (nums[i] + nums[j] === target) {
34
+ return [i, j];
35
36
37
38
39
40
41
+*/
0 commit comments