File tree 5 files changed +130
-0
lines changed
5 files changed +130
-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
+ function backtrack ( start , curr , sum ) {
9
+ // 현재 합계가 타겟과 같으면 결과에 추가
10
+ if ( sum === target ) {
11
+ result . push ( [ ...curr ] )
12
+ }
13
+
14
+ // 합계가 타겟을 초과하면 더 이상 진행하지 않음
15
+ if ( sum > target ) {
16
+ return
17
+ }
18
+
19
+ // 현재 인덱스부터 시작하여 모든 후보를 시도
20
+ for ( let i = start ; i < candidates . length ; i ++ ) {
21
+ curr . push ( candidates [ i ] )
22
+ // 같은 숫자를 여러 번 사용할 수 있으므로 i부터 다시 시작
23
+ backtrack ( i , curr , sum + candidates [ i ] )
24
+ curr . pop ( )
25
+ }
26
+ }
27
+ backtrack ( 0 , [ ] , 0 )
28
+ return result
29
+ } ;
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
+ if ( s [ 0 ] === '0' ) return 0 ;
7
+
8
+ let count = 0 ;
9
+ const memo = { } ;
10
+
11
+ function backtrack ( index , curr ) {
12
+ // 기저 조건: 문자열 끝까지 왔으면 유효한 디코딩 발견
13
+ if ( index === s . length ) {
14
+ count ++ ;
15
+ return ;
16
+ }
17
+
18
+ // 메모이제이션 키 생성
19
+ const key = index ;
20
+ if ( memo [ key ] !== undefined ) {
21
+ count += memo [ key ] ;
22
+ return ;
23
+ }
24
+
25
+ // 이전 카운트 저장
26
+ const prevCount = count ;
27
+
28
+ // Case 1: 한 자리 숫자로 디코딩 (1~9)
29
+ if ( s [ index ] !== '0' ) {
30
+ const oneDigit = s [ index ] ;
31
+ curr . push ( oneDigit ) ;
32
+ backtrack ( index + 1 , curr ) ;
33
+ curr . pop ( ) ;
34
+ }
35
+
36
+ // Case 2: 두 자리 숫자로 디코딩 (10~26)
37
+ if ( index + 1 < s . length ) {
38
+ const twoDigit = s . substring ( index , index + 2 ) ;
39
+ const num = parseInt ( twoDigit ) ;
40
+ if ( num >= 10 && num <= 26 ) {
41
+ curr . push ( twoDigit ) ;
42
+ backtrack ( index + 2 , curr ) ;
43
+ curr . pop ( ) ;
44
+ }
45
+ }
46
+
47
+ // 현재 위치에서 발견한 디코딩 방법 수 저장
48
+ memo [ key ] = count - prevCount ;
49
+ }
50
+
51
+ backtrack ( 0 , [ ] ) ;
52
+ return count ;
53
+ } ;
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 = - Infinity
7
+ let currentSum = 0
8
+
9
+ for ( let num of nums ) {
10
+ currentSum = Math . max ( num , currentSum + num )
11
+ maxSum = Math . max ( maxSum , currentSum )
12
+ }
13
+
14
+ return maxSum
15
+ } ;
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
+ function recursive ( num ) {
7
+ // 종료조건
8
+ if ( String ( num ) === '1' ) {
9
+ return '1'
10
+ }
11
+ // 재귀호출
12
+ const q = Math . floor ( num / 2 ) // 몫
13
+ const r = num % 2 // 나머지
14
+ const total = r + recursive ( q )
15
+
16
+ // 데이터 통합
17
+ return total
18
+ }
19
+ const binaryString = recursive ( n )
20
+ const result = [ ...binaryString ] . map ( Number ) . reduce ( ( a , b ) => a + b , 0 )
21
+
22
+ return result
23
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {boolean }
4
+ */
5
+ var isPalindrome = function ( s ) {
6
+ const cleanStr = s . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] / g, '' )
7
+ const reversedStr = [ ...cleanStr ] . reverse ( ) . join ( '' )
8
+
9
+ return cleanStr === reversedStr
10
+ } ;
You can’t perform that action at this time.
0 commit comments