File tree Expand file tree Collapse file tree 5 files changed +133
-0
lines changed Expand file tree Collapse file tree 5 files changed +133
-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
+ function backtrack ( remaining , combination , start ) {
10
+ if ( remaining === 0 ) {
11
+ result . push ( [ ...combination ] ) ;
12
+ return ;
13
+ }
14
+ if ( remaining < 0 ) return ;
15
+
16
+ for ( let i = start ; i < candidates . length ; i ++ ) {
17
+ combination . push ( candidates [ i ] ) ;
18
+ backtrack ( remaining - candidates [ i ] , combination , i ) ; // 같은 숫자 다시 사용 가능
19
+ combination . pop ( ) ; // backtrack
20
+ }
21
+ }
22
+
23
+ backtrack ( target , [ ] , 0 ) ;
24
+ return result ;
25
+ } ;
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 || s [ 0 ] === "0" ) return 0 ;
7
+
8
+ const n = s . length ;
9
+ const dp = Array ( n + 1 ) . fill ( 0 ) ;
10
+
11
+ dp [ 0 ] = 1 ; // 빈 문자열은 1가지 방법
12
+ dp [ 1 ] = 1 ; // 첫 글자가 0이 아니면 1가지 방법
13
+
14
+ for ( let i = 2 ; i <= n ; i ++ ) {
15
+ const oneDigit = parseInt ( s . slice ( i - 1 , i ) ) ;
16
+ const twoDigits = parseInt ( s . slice ( i - 2 , i ) ) ;
17
+
18
+ if ( oneDigit >= 1 && oneDigit <= 9 ) {
19
+ dp [ i ] += dp [ i - 1 ] ;
20
+ }
21
+ if ( twoDigits >= 10 && twoDigits <= 26 ) {
22
+ dp [ i ] += dp [ i - 2 ] ;
23
+ }
24
+ }
25
+
26
+ return dp [ n ] ;
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
+ // 초기값 설정: 현재까지의 최대합과 전체 최대합을 배열의 첫 번째 값으로 초기화
7
+ let currentSum = nums [ 0 ] ;
8
+ let maxSum = nums [ 0 ] ;
9
+
10
+ // 두 번째 원소부터 순회
11
+ for ( let i = 1 ; i < nums . length ; i ++ ) {
12
+ // 이전까지의 합에 현재 원소를 더할지, 아니면 현재 원소부터 새로 시작할지 결정
13
+ currentSum = Math . max ( nums [ i ] , currentSum + nums [ i ] ) ;
14
+ // 전체 최대값 갱신
15
+ maxSum = Math . max ( maxSum , currentSum ) ;
16
+ }
17
+
18
+ return maxSum ;
19
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } n
3
+ * @return {number }
4
+ */
5
+ //1. divide the number by 2 and count the remainder
6
+ var hammingWeight = function ( n ) {
7
+ let count = 0 ;
8
+ while ( n > 0 ) {
9
+ if ( n % 2 === 1 ) {
10
+ count ++ ;
11
+ }
12
+ n = Math . floor ( n / 2 ) ;
13
+ }
14
+ return count ;
15
+ } ;
16
+
17
+ //2. Count the number of set bits (1s) in the binary representation of n
18
+ var hammingWeight = function ( n ) {
19
+ return n . toString ( 2 ) . split ( "1" ) . length - 1 ;
20
+ } ;
21
+
22
+ //3. bit manipulation
23
+ var hammingWeight = function ( n ) {
24
+ let count = 0 ;
25
+ while ( n > 0 ) {
26
+ count += n & 1 ; // 마지막 비트가 1이면 count++
27
+ n = n >>> 1 ; // 오른쪽으로 한 비트 이동 (2로 나눔)
28
+ }
29
+ return count ;
30
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {boolean }
4
+ */
5
+
6
+ // 1. Two Pointers
7
+ // time complexity: O(n)
8
+ // space complexity: O(1)
9
+ var isPalindrome = function ( s ) {
10
+ let sRefine = s . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] / g, "" ) ;
11
+ let left = 0 ;
12
+ let right = sRefine . length - 1 ;
13
+
14
+ while ( left < right ) {
15
+ if ( sRefine [ left ] !== sRefine [ right ] ) {
16
+ return false ;
17
+ }
18
+ left ++ ;
19
+ right -- ;
20
+ }
21
+
22
+ return true ;
23
+ } ;
24
+
25
+ // 2. String Manipulation
26
+ // time complexity: O(n)
27
+ // space complexity: O(n)
28
+ var isPalindrome = function ( s ) {
29
+ let refined = s . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] / g, "" ) ;
30
+ let reversed = refined . split ( "" ) . reverse ( ) . join ( "" ) ;
31
+ return refined === reversed ;
32
+ } ;
You can’t perform that action at this time.
0 commit comments