File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * @description nums 배열에서 중복 숫자 확인
3+ * @param nums - 숫자 배열
4+ * @returns boolean - 중복 숫자 여부
5+ */
6+ const containsDuplicate = ( nums : number [ ] ) => {
7+ const hasSeen = new Set < number > ( ) ;
8+
9+ for ( const num of nums ) {
10+ if ( hasSeen . has ( num ) ) {
11+ return true ;
12+ }
13+ hasSeen . add ( num ) ;
14+ }
15+ return false ;
16+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @description nums 배열에서 두 수를 더해 target을 만족하는 인덱스를 반환
3+ * @param {number[] } nums - 숫자 배열
4+ * @param {number } target - 목표 숫자
5+ * @return {number[] } - 두 수의 인덱스
6+ */
7+ var twoSum = function ( nums , target ) {
8+ const map = new Map ( ) ;
9+
10+ for ( let i = 0 ; i < nums . length ; i ++ ) {
11+ const current = nums [ i ] ;
12+ const needed = target - current ;
13+ if ( map . has ( needed ) ) {
14+ return [ map . get ( needed ) , i ] ;
15+ }
16+ map . set ( current , i ) ;
17+ }
18+ } ;
You can’t perform that action at this time.
0 commit comments