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 22f71ca commit 829fe66Copy full SHA for 829fe66
โtwo-sum/soobing2.tsโ
@@ -0,0 +1,25 @@
1
+/**
2
+ * ๋ฌธ์ ์ ํ
3
+ * - Array
4
+ *
5
+ * ๋ฌธ์ ์ค๋ช
6
+ * - ์ฃผ์ด์ง ๋ฐฐ์ด์์ ๋ ์๋ฅผ ๋ํด์ target ๊ฐ์ด ๋๋ ์ธ๋ฑ์ค๋ฅผ ๋ฐํ
7
8
+ * ์์ด๋์ด
9
+ * - ์ํํ๋ฉด์ map์ ๋จผ์ ๋ง๋ค์ด๋๊ณ , ๋ค์ ์ํํ๋ฉด์ target-nums[i] ๊ฐ์ด ์๋์ง ํ์ธ
10
+ */
11
+function twoSum(nums: number[], target: number): number[] {
12
+ const map = new Map();
13
+ for (let i = 0; i < nums.length; i++) {
14
+ map.set(nums[i], i);
15
+ }
16
+
17
18
+ const index = map.get(target - nums[i]);
19
+ if (i === index || index === undefined) continue;
20
21
+ return [i, index];
22
23
24
+ return [];
25
+}
0 commit comments