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 0e5ffe5 commit a6a8263Copy full SHA for a6a8263
1 file changed
โtwo-sum/Yu-Won.jsโ
@@ -0,0 +1,25 @@
1
+/**
2
+ * ๋ฌธ์ : https://leetcode.com/problems/two-sum/
3
+ *
4
+ * ์๊ตฌ์ฌํญ:
5
+ * nums: number[], target: number ๋ฅผ Input ์ผ๋ก ๋ฐ์์ ๋
6
+ * nums์ ์๋ ๊ฐ ๋๊ฐ๋ฅผ ๋ํ์ ๋ target ๊ณผ ์ ํํ๊ฒ ์ผ์นํ๊ฒ ๋๋ ๊ฐ์ ๋ง๋ค๊ฒ ๋๋ ์ธ๋ฑ์ค ๋ฐฐ์ด์ ๋ฆฌํดํ๋ค.
7
8
+ * ํด์๋งต ์ด์ฉ
9
+ * */
10
+
11
+const twoSum = (nums, target) => {
12
+ const map = new Map();
13
14
+ for(let i = 0; i < nums.length; i++) {
15
+ const result = target - nums[i];
16
17
+ // ์ด๋ฏธ ๊ฐ์ด ์์๋ค๋ฉด ๋ฆฌํด
18
+ if(map.has(result)) {
19
+ return [map.get(result), i];
20
+ }
21
22
+ // ํ์ฌ ๊ฐ์ ์ ์ฅ
23
+ map.set(nums[i], i);
24
25
+}
0 commit comments