File tree Expand file tree Collapse file tree 5 files changed +169
-0
lines changed Expand file tree Collapse file tree 5 files changed +169
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ *
3+ * @param candidates
4+ * @param target
5+ *
6+ * backtracking ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ๋ฌธ์ ํด๊ฒฐ
7+ *
8+ */
9+
10+ function combinationSum ( candidates : number [ ] , target : number ) : number [ ] [ ] {
11+ const result : number [ ] [ ] = [ ] ;
12+ if ( candidates . length === 0 ) {
13+ return result ;
14+ }
15+
16+ candidates . sort ( ( a , b ) => a - b ) ;
17+
18+ const validCandidates : number [ ] = candidates . filter ( num => num <= target ) ;
19+
20+ if ( validCandidates . length === 0 ) {
21+ return result ;
22+ }
23+
24+ const currentCombination : number [ ] = [ ] ;
25+
26+ function backtrack ( startIndex : number , remainingTarget : number ) :void {
27+ if ( remainingTarget === 0 ) {
28+ result . push ( [ ...currentCombination ] ) ;
29+ return ;
30+ }
31+
32+ for ( let i = startIndex ; i < validCandidates . length ; i ++ ) {
33+ const currentNum = validCandidates [ i ] ;
34+
35+ if ( currentNum > remainingTarget ) {
36+ break ;
37+ }
38+ currentCombination . push ( currentNum ) ;
39+
40+ backtrack ( i , remainingTarget - currentNum )
41+
42+ currentCombination . pop ( )
43+
44+ }
45+ }
46+
47+ backtrack ( 0 , target ) ;
48+ return result ;
49+ } ;
Original file line number Diff line number Diff line change 1+ function numDecodings ( s : string ) : number {
2+ // ๋น ๋ฌธ์์ด์ด๊ฑฐ๋ 0์ผ๋ก ์์ํ๋ฉด ๋์ฝ๋ฉ ๋ถ๊ฐ
3+ if ( ! s || s [ 0 ] === '0' ) return 0 ;
4+
5+ const n = s . length ;
6+
7+ // ๋ฌธ์์ด ๊ธธ์ด๊ฐ 1์ด๋ฉด ๋ฐ๋ก ๊ฒฐ๊ณผ ๋ฐํ
8+ if ( n === 1 ) return 1 ;
9+
10+ // ์ด๊ธฐ ์ํ
11+ let prev = 1 ; // dp[0]
12+ let curr = s [ 0 ] === '0' ? 0 : 1 ; // dp[1]
13+
14+ for ( let i = 2 ; i <= n ; i ++ ) {
15+ let temp = 0 ;
16+ const oneDigit = parseInt ( s [ i - 1 ] ) ;
17+ const twoDigit = parseInt ( s [ i - 2 ] + s [ i - 1 ] ) ;
18+
19+ // ํ ์๋ฆฌ ์ซ์๋ก ๋์ฝ๋ฉ (1-9)
20+ if ( oneDigit >= 1 ) {
21+ temp += curr ;
22+ }
23+
24+ // ๋ ์๋ฆฌ ์ซ์๋ก ๋์ฝ๋ฉ (10-26)
25+ if ( twoDigit >= 10 && twoDigit <= 26 ) {
26+ temp += prev ;
27+ }
28+
29+ // ์ํ ์
๋ฐ์ดํธ
30+ prev = curr ;
31+ curr = temp ;
32+ }
33+
34+ return curr ;
35+ }
Original file line number Diff line number Diff line change 1+ function maxSubArray ( nums : number [ ] ) : number {
2+ // ๋ฐฐ์ด์ด ๋น์ด ์๋ ๊ฒฝ์ฐ ์ฒดํฌ (์ ์ฝ์กฐ๊ฑด์ ์ํด ๋ฐ์ํ์ง ์์ง๋ง, ๊ฒฌ๊ณ ํ ์ฝ๋๋ฅผ ์ํด)
3+ if ( nums . length === 0 ) return 0 ;
4+
5+ // ํ์ฌ ๋ถ๋ถ ๋ฐฐ์ด์ ํฉ๊ณผ ์ ์ฒด ์ต๋ ๋ถ๋ถ ๋ฐฐ์ด ํฉ ์ด๊ธฐํ
6+ let currentSum = nums [ 0 ] ;
7+ let maxSum = nums [ 0 ] ;
8+
9+ // ๋ ๋ฒ์งธ ์์๋ถํฐ ์ํ
10+ for ( let i = 1 ; i < nums . length ; i ++ ) {
11+ // ํ์ฌ ์์น์์์ ์ต๋ ๋ถ๋ถ ๋ฐฐ์ด ํฉ ๊ณ์ฐ
12+ // "์ด์ ๊น์ง์ ํฉ + ํ์ฌ ์์" vs "ํ์ฌ ์์๋ถํฐ ์๋ก ์์"
13+ currentSum = Math . max ( nums [ i ] , currentSum + nums [ i ] ) ;
14+
15+ // ์ ์ฒด ์ต๋๊ฐ ์
๋ฐ์ดํธ
16+ maxSum = Math . max ( maxSum , currentSum ) ;
17+ }
18+
19+ return maxSum ;
20+ }
Original file line number Diff line number Diff line change 1+ /**
2+ *
3+ * @param n
4+ *
5+ * ํ์ด 1
6+ *
7+ * function hammingWeight(n: number): number {
8+ * let parse = (n).toString(2);
9+ * let count = 0;
10+ * for (const item of parse){
11+ * if(+item ===1) count ++
12+ * }
13+ * return count
14+ * };
15+ *
16+ * ์ซ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ ๋ ์ค๋ฒํค๋ ๋ฐ์
17+ * ๋นํธ ์ฐ์ฐ์ ์ฌ์ฉํด์ผํ ๋ฏ!
18+ *
19+ * ๊ฒ์ํด๋ณด๋ Brian Kernighan ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ๋ฉด ๋ ํจ์จ์ ์ด๋ผ๊ณ ํ๋ค
20+ */
21+
22+ function hammingWeight ( n : number ) : number {
23+ let count = 0 ;
24+
25+ while ( n !== 0 ) {
26+ n &= ( n - 1 ) ;
27+ count ++ ;
28+ }
29+
30+ return count ;
31+ }
Original file line number Diff line number Diff line change 1+ /**
2+ *
3+ * @param s
4+ *
5+ * ํ์ด 1
6+ *
7+ * ํน์๋ฌธ์ ์ ๊ท ํํ์์ด ๋ณต์กํ๊ณ , ๋ถํ ๊ณผ ํฉ์น๋ ๊ณผ์ ์ด ์ค๋ณต๋๋ค
8+ *
9+ * function isPalindrome(s: string): boolean {
10+ * const reg= /[\{\}\[\]\/?.,;:|\)*~`!^\-_+<>@\#$%&\\\=\(\'\"]/gi;
11+ * let palindrome= s.replace(reg,'').toLowerCase().split('');
12+ *
13+ * return palindrome.join('').replace(/ /g,"")===palindrome.reverse().join('').replace(/ /g,"")
14+ * };
15+ *
16+ * ๊ทธ๋์ ์๊ฐํ ํ์ด 2๋ s consists only of printable ASCII characters. ์ ๋ณด๊ณ ์ซ์์ ์ํ๋ฒณ์ ์ ์ธํ๊ณ ๋๋จธ์ง๋ ์ ๊ฑฐํ๊ณ
17+ * ํฌํฌ์ธํธ ๋ฐฉ๋ฒ์ผ๋ก ๋ณ๊ฒฝํด์ ๋ฌธ์ ํด๊ฒฐ
18+ */
19+
20+ function isPalindrome ( s : string ) : boolean {
21+ const cleanStr = s . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] / g, '' ) ;
22+
23+ let left = 0 ;
24+ let right = cleanStr . length - 1 ;
25+
26+ while ( left < right ) {
27+ if ( cleanStr [ left ] !== cleanStr [ right ] ) {
28+ return false ;
29+ }
30+ left ++ ;
31+ right -- ;
32+ }
33+ return true
34+ } ;
You canโt perform that action at this time.
0 commit comments