File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ๋ฌธ์ ํ์
:
3+ * ์ ์ ๋ฐฐ์ด nums์ ๋ชฉํ๊ฐ target์ด ์ฃผ์ด์ก์ ๋, ๋ ์ซ์๋ฅผ ๋ํด์ target์ด ๋๋ ๋ ์์ ์ธ๋ฑ์ค๋ฅผ ์ฐพ๋ ๋ฌธ์
4+ * follow-up: ๋ธ๋ฃจํธ ํฌ์ค(์ด์ค ๋ฐ๋ณต๋ฌธ -> O(n^2)) ์ ๊ทผ๋ฒ๋ณด๋ค ํจ์จ์ ์ธ ํด์๋งต์ ์ฌ์ฉํ์ฌ ํด๊ฒฐ
5+ * => ๋ฐฐ์ด์ ํ ๋ฒ๋ง ์ํํ๊ธฐ ๋๋ฌธ์ ์ ํ ์๊ฐ ๋ณต์ก๋ O(n)
6+ *
7+ * ์ ๊ทผ ๋ฐฉ์:
8+ * (1) ์ซ์์ ํด๋น ์ธ๋ฑ์ค๋ฅผ ์ ์ฅํ๊ธฐ ์ํด Map ๊ฐ์ฒด ์ฌ์ฉ
9+ * (2) ๋ฐฐ์ด์ ํ ๋ฒ๋ง ์ํํ๋ฉด์ ๊ฐ ์์๋ฅผ ํ์ธํ๋๋ฐ,
10+ * (3) ๊ฐ ์ซ์์ ๋ํด, target - ํ์ฌ ์ซ์๊ฐ ์ด๋ฏธ ํด์๋งต์ ์๋์ง ํ์ธํด์
11+ * ์๋ค๋ฉด: ์ฐพ์ ์ซ์์ ํ์ฌ ์ซ์์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํํ๊ณ
12+ * ์๋ค๋ฉด: ํ์ฌ ์ซ์์ ๊ทธ ์ธ๋ฑ์ค๋ฅผ ํด์๋งต์ ์ ์ฅํ๊ณ ๊ณ์ ์งํ
13+ */
14+
15+ /**
16+ * @param {number[] } nums - ์ ์ ๋ฐฐ์ด
17+ * @param {number } target - ๋ชฉํ ํฉ๊ณ ๊ฐ
18+ * @return {number[] } - ํฉ์ด ๋ชฉํ๊ฐ์ด ๋๋ ๋ ์์ ์ธ๋ฑ์ค
19+ */
20+ var twoSum = function ( nums , target ) {
21+ const numMap = new Map ( ) ;
22+
23+ for ( let i = 0 ; i < nums . length ; i ++ ) {
24+ const currentNum = nums [ i ] ;
25+
26+ const complement = target - currentNum ;
27+
28+ if ( numMap . has ( complement ) ) {
29+ return [ numMap . get ( complement ) , i ] ;
30+ }
31+
32+ numMap . set ( currentNum , i ) ;
33+ }
34+
35+ return [ ] ;
36+ } ;
You canโt perform that action at this time.
0 commit comments