File tree Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 1+ function combinationSum ( candidates : number [ ] , target : number ) : number [ ] [ ] {
2+ // arr: index까지 갈 수 있는 combinationSum
3+ const arr :number [ ] [ ] [ ] = Array . from ( { length : target + 1 } , ( ) => [ ] as number [ ] [ ] ) ;
4+ // 0을 만들 수 있는 방법은 숫자가 없는 것
5+ arr [ 0 ] . push ( [ ] as number [ ] ) ;
6+
7+ for ( const candidate of candidates ) {
8+ for ( let n = candidate ; n <= target ; n ++ ) {
9+ for ( const combination of arr [ n - candidate ] ) {
10+ arr [ n ] . push ( [ ...combination , candidate ] ) ;
11+ }
12+ }
13+ }
14+ console . log ( arr ) ;
15+ return arr [ target ] ;
16+ } ;
Original file line number Diff line number Diff line change 1+ function hammingWeight ( n : number ) : number {
2+ // 최대 이진수 찾기
3+ let s = 1 ;
4+ while ( s * 2 <= n ) {
5+ s *= 2 ;
6+ }
7+
8+ // bit 세기
9+ let cnt = 0 ;
10+ while ( n > 0 ) {
11+ if ( n - s >= 0 ) {
12+ n -= s ;
13+ cnt ++ ;
14+ }
15+ s /= 2 ;
16+ }
17+ return cnt ;
18+ } ;
Original file line number Diff line number Diff line change 1+ function isPalindrome ( s : string ) : boolean {
2+ // console.log('A'.charCodeAt(0), 'Z'.charCodeAt(0)); // 65 90
3+ // console.log('a'.charCodeAt(0), 'z'.charCodeAt(0)); // 97 122
4+ // console.log('0'.charCodeAt(0), '9'.charCodeAt(0)); // 48 57
5+
6+ // 문자열 변환 과정
7+ let converted = ''
8+ for ( let c of s ) {
9+ const charCode = c . charCodeAt ( 0 ) ;
10+ if ( charCode >= 65 && charCode <= 90 ) {
11+ converted += c . toLowerCase ( ) ;
12+ }
13+ else if ( ( charCode >= 97 && charCode <= 122 ) || ( charCode >= 48 && charCode <= 57 ) ) {
14+ converted += c ;
15+ }
16+ }
17+
18+ // palindrome 판단 조건
19+ const length = converted . length ;
20+
21+ for ( let i = 0 ; i < length / 2 ; i ++ ) {
22+ if ( converted [ i ] !== converted [ length - 1 - i ] ) return false ;
23+ }
24+
25+ return true ;
26+ } ;
You can’t perform that action at this time.
0 commit comments