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+ * @param {number[] } candidates
3+ * @param {number } target
4+ * @return {number[][] }
5+ */
6+ function combinationSum ( candidates , target ) {
7+ var buffer = [ ] ;
8+ var result = [ ] ;
9+ search ( 0 , target ) ;
10+ return result ;
11+
12+ function search ( startIdx , target ) {
13+ if ( target === 0 ) return result . push ( buffer . slice ( ) ) ;
14+ if ( target < 0 ) return ;
15+ if ( startIdx === candidates . length ) return ;
16+ buffer . push ( candidates [ startIdx ] ) ;
17+ search ( startIdx , target - candidates [ startIdx ] ) ;
18+ buffer . pop ( ) ;
19+ search ( startIdx + 1 , target ) ;
20+ }
21+ } ;
22+
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' ) {
7+ return 0 ;
8+ }
9+
10+ const n = s . length ;
11+ const dp = new Array ( n + 1 ) . fill ( 0 ) ;
12+ dp [ 0 ] = 1 ;
13+ dp [ 1 ] = 1 ;
14+
15+ for ( let i = 2 ; i <= n ; ++ i ) {
16+ const oneDigit = parseInt ( s [ i - 1 ] ) ;
17+ const twoDigits = parseInt ( s . substring ( i - 2 , i ) ) ;
18+
19+ if ( oneDigit !== 0 ) {
20+ dp [ i ] += dp [ i - 1 ] ;
21+ }
22+
23+ if ( 10 <= twoDigits && twoDigits <= 26 ) {
24+ dp [ i ] += dp [ i - 2 ] ;
25+ }
26+ }
27+
28+ return dp [ n ] ;
29+ } ;
30+
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+ if ( ! nums || nums . length === 0 ) {
7+ return 0 ;
8+ }
9+
10+ // max_so_far: overall maximum sum
11+ let max_so_far = nums [ 0 ] ;
12+
13+ // max_ending_here: max sum of subarray ending at current position
14+ let max_ending_here = nums [ 0 ] ;
15+
16+ for ( let i = 1 ; i < nums . length ; i ++ ) {
17+ // Core decision: start new or extend previous
18+ max_ending_here = Math . max ( nums [ i ] , max_ending_here + nums [ i ] ) ;
19+
20+ // Update overall maximum
21+ max_so_far = Math . max ( max_so_far , max_ending_here ) ;
22+ }
23+
24+ return max_so_far ;
25+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number } n - 32๋นํธ ์ ์
3+ * @return {number } - ์ด์ง์์์ 1์ ๊ฐ์
4+ */
5+ var hammingWeight = function ( n ) {
6+ let setBitCount = 0 ; // 1์ ๊ฐ์๋ฅผ ์ ์ฅํ ๋ณ์
7+
8+ // n์ด 0์ด ๋ ๋๊น์ง ๋ฐ๋ณต (๋ชจ๋ 1๋นํธ๋ฅผ ์ ๊ฑฐํ ๋๊น์ง)
9+ while ( n !== 0 ) {
10+ // ๊ฐ์ฅ ์ค๋ฅธ์ชฝ์ ์๋ 1๋นํธ๋ฅผ ์ ๊ฑฐ
11+ // ์: 101100 -> 101000
12+ n &= ( n - 1 ) ;
13+
14+ // 1๋นํธ ํ๋ ์ ๊ฑฐํ์ผ๋ฏ๋ก ์นด์ดํธ ์ฆ๊ฐ
15+ setBitCount ++ ;
16+ }
17+
18+ // ์ด 1์ ๊ฐ์ ๋ฐํ
19+ return setBitCount ;
20+ } ;
21+
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+ // 1. ์๋ฌธ์๋ก ๋ณํ + ์ํ๋ฒณ/์ซ์๋ง ๋จ๊ธฐ๊ธฐ
7+ // (๊ณต๋ฐฑ, ํน์๋ฌธ์ ์ ๊ฑฐ)
8+ s = s . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] / g, "" ) ;
9+
10+ const length = s . length ;
11+
12+ // 2. ๋ฌธ์์ด์ ์ ๋ฐ๋ง ์ํํ๋ฉด์ ์์ชฝ ๋น๊ต
13+ for ( let i = 0 ; i < length / 2 ; i ++ ) {
14+ // ์ผ์ชฝ(i) vs ์ค๋ฅธ์ชฝ(length - 1 - i)
15+ if ( s [ i ] !== s [ length - 1 - i ] ) {
16+ // ํ๋๋ผ๋ ๋ค๋ฅด๋ฉด ํฐ๋ฆฐ๋๋กฌ ์๋
17+ return false ;
18+ }
19+ }
20+
21+ // 3. ๋๊น์ง ๋ฌธ์ ์์ผ๋ฉด ํฐ๋ฆฐ๋๋กฌ
22+ return true ;
23+ } ;
You canโt perform that action at this time.
0 commit comments