File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ์ฃผ์ด์ง ๋ฐฐ์ด์์ ๋ ์ซ์์ ํฉ์ด target์ด ๋๋ idx ์์ ๋ฐํํ๋ ํจ์
3+ * - ์๊ฐ ๋ณต์ก๋: O(n)
4+ * - ํ๋ฒ์ ๋ฐฐ์ด์ ์ํํ๋ฉฐ Map์ ๊ฐ์ ์ ์ฅํ๊ณ , Map์์ ๊ฐ์ ์ฐพ์
5+ * - ๊ณต๊ฐ ๋ณต์ก๋: O(n)
6+ * - ์ซ์์ ๊ทธ๋์ idx๋ฅผ ์์ผ๋กํ๋ Map
7+ *
8+ * @param {number[] } nums - ์ซ์ ๋ฐฐ์ด
9+ * @param {number } target - ํ๊ฒ ํฉ
10+ * @returns {number[] } - ํฉ์ ๋ง๋ค ์ ์๋ idx ๋ฐฐ์ด
11+ */
12+ function twoSum ( nums : number [ ] , target : number ) : number [ ] {
13+ const map = new Map < number , number > ( ) ;
14+ for ( let i = 0 ; i < nums . length ; i ++ ) {
15+ const cur = nums [ i ] ; // ํ์ฌ ๊ฐ
16+ const reamin = target - cur ; // ๋๋จธ์ง
17+ if ( map . has ( reamin ) ) {
18+ // Non-null assertion operator(!)๋ฅผ ์ฌ์ฉํ์ฌ undefined๊ฐ ์๋์ ๋จ์ธ
19+ return [ map . get ( reamin ) ! , i ] ;
20+ }
21+ // ๋๋จธ์ง๋ฅผ ์ฐพ์ง ๋ชปํ ๊ฒฝ์ฐ ํ์ฌ ๊ฐ์ ์ ์ฅ
22+ map . set ( cur , i ) ;
23+ }
24+ return [ ] ; // ๋ชฉํ ํฉ์ ๋ง๋ค ์ ์๋ ์ซ์๊ฐ ์๋ ๊ฒฝ์ฐ ๋น ๋ฐฐ์ด ๋ฐํ
25+ }
You canโt perform that action at this time.
0 commit comments