File tree Expand file tree Collapse file tree 5 files changed +92
-0
lines changed Expand file tree Collapse file tree 5 files changed +92
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } candidates
3+ * @param {number } target
4+ * @return {number[][] }
5+ */
6+ var combinationSum = function ( candidates , target ) {
7+ const result = [ ] ;
8+
9+ const dfs = ( start , path , sum ) => {
10+ if ( sum === target ) {
11+ result . push ( [ ...path ] ) ; // 정답 조합 발견
12+ return ;
13+ }
14+
15+ if ( sum > target ) return ; // target 초과 -> 백트랙
16+
17+ for ( let i = start ; i < candidates . length ; i ++ ) {
18+ path . push ( candidates [ i ] ) ; // 숫자 선택
19+ dfs ( i , path , sum + candidates [ i ] ) ; // 같은 인덱스부터 다시 탐색 (중복 사용 허용)
20+ path . pop ( ) ; // 백트래킹
21+ }
22+ } ;
23+
24+ dfs ( 0 , [ ] , 0 ) ;
25+ return result ;
26+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @return {number }
4+ */
5+ var numDecodings = function ( s ) {
6+ const n = s . length ;
7+ const memo = { } ;
8+
9+ function dfs ( index ) {
10+ if ( index === n ) return 1 ;
11+ if ( s [ index ] === '0' ) return 0 ;
12+ if ( memo . hasOwnProperty ( index ) ) return memo [ index ] ;
13+
14+ let count = dfs ( index + 1 ) ;
15+ if ( index + 1 < n ) {
16+ const twoDigit = parseInt ( s . slice ( index , index + 2 ) ) ;
17+ if ( twoDigit >= 10 && twoDigit <= 26 ) {
18+ count += dfs ( index + 2 ) ;
19+ }
20+ }
21+
22+ memo [ index ] = count ;
23+ return count ;
24+ }
25+
26+ return dfs ( 0 ) ;
27+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @return {number }
4+ */
5+ var maxSubArray = function ( nums ) {
6+ let maxSum = nums [ 0 ] ;
7+ let currentSum = nums [ 0 ] ;
8+
9+ for ( let i = 1 ; i < nums . length ; i ++ ) {
10+ // 지금 원소 자체가 더 클 수도 있음 (부분합 리셋)
11+ currentSum = Math . max ( nums [ i ] , currentSum + nums [ i ] ) ;
12+ maxSum = Math . max ( maxSum , currentSum ) ;
13+ }
14+
15+ return maxSum ;
16+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number } n
3+ * @return {number }
4+ */
5+ var hammingWeight = function ( n ) {
6+ let list = n . toString ( 2 ) . split ( '' ) . map ( Number ) ;
7+ let sum = 0 ;
8+ list . forEach ( ( m ) => {
9+ if ( m == 1 ) {
10+ sum += 1 ; a
11+ }
12+ } )
13+ return sum ;
14+ } ;
Original file line number Diff line number Diff line change 1+ function filterStr ( inputString ) {
2+ return inputString . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '' ) . toLowerCase ( ) ;
3+ }
4+
5+ var isPalindrome = function ( s ) {
6+ const filtered = filterStr ( s ) ;
7+ const reversed = filtered . split ( '' ) . reverse ( ) . join ( '' ) ;
8+ return filtered === reversed ;
9+ } ;
You can’t perform that action at this time.
0 commit comments