File tree Expand file tree Collapse file tree 5 files changed +125
-0
lines changed
Expand file tree Collapse file tree 5 files changed +125
-0
lines changed Original file line number Diff line number Diff line change 1+ function combinationSum ( candidates : number [ ] , target : number ) : number [ ] [ ] {
2+ const results : number [ ] [ ] = [ ] ;
3+
4+ function comb ( index : number , arr : number [ ] , sum : number ) {
5+ if ( sum === target ) {
6+ results . push ( [ ...arr ] ) ;
7+ return ;
8+ }
9+ if ( sum > target || candidates . length <= index ) {
10+ return ;
11+ }
12+ arr . push ( candidates [ index ] ) ;
13+ comb ( index , arr , sum + candidates [ index ] ) ;
14+ arr . pop ( ) ;
15+ comb ( index + 1 , arr , sum ) ;
16+ }
17+
18+ comb ( 0 , [ ] , 0 ) ;
19+ return results ;
20+ }
Original file line number Diff line number Diff line change 1+ function numDecodings ( s : string ) : number {
2+ if ( s . length === 1 && s [ 0 ] !== "0" ) {
3+ return 1 ;
4+ }
5+ if ( s [ 0 ] === "0" ) {
6+ return 0 ;
7+ }
8+ const dp = new Array ( s . length + 1 ) . fill ( 0 ) ;
9+ dp [ 0 ] = 1 ;
10+ dp [ 1 ] = 1 ;
11+ for ( let i = 2 ; i <= s . length ; i ++ ) {
12+ if ( s [ i - 1 ] !== "0" ) {
13+ dp [ i ] += dp [ i - 1 ] ;
14+ }
15+ const doubleNum = parseInt ( s [ i - 2 ] + s [ i - 1 ] ) ;
16+ if ( doubleNum >= 10 && doubleNum <= 26 ) {
17+ dp [ i ] += dp [ i - 2 ] ;
18+ }
19+ }
20+ return dp [ s . length ] ;
21+ }
Original file line number Diff line number Diff line change 1+ function maxSubArray ( nums : number [ ] ) : number {
2+ function subSums ( arr : number [ ] , left : number , right : number ) {
3+ if ( left === right ) {
4+ return nums [ left ] ;
5+ }
6+ const middle = Math . floor ( ( left + right ) / 2 ) ;
7+
8+ const leftMax = subSums ( arr , left , middle ) ;
9+ const rightMax = subSums ( arr , middle + 1 , right ) ;
10+ let leftSum = 0 ;
11+ let leftMaxSum = Number . NEGATIVE_INFINITY ;
12+ for ( let i = middle ; i >= left ; i -- ) {
13+ leftSum += arr [ i ] ;
14+ if ( leftMaxSum < leftSum ) {
15+ leftMaxSum = leftSum ;
16+ }
17+ }
18+ let rightSum = 0 ;
19+ let rightMaxSum = Number . NEGATIVE_INFINITY ;
20+ for ( let i = middle + 1 ; i <= right ; i ++ ) {
21+ rightSum += arr [ i ] ;
22+ if ( rightMaxSum < rightSum ) {
23+ rightMaxSum = rightSum ;
24+ }
25+ }
26+
27+ return Math . max ( leftMaxSum + rightMaxSum , leftMax , rightMax ) ;
28+ }
29+ return subSums ( nums , 0 , nums . length - 1 ) ;
30+ }
Original file line number Diff line number Diff line change 1+ function hammingWeight ( n : number ) : number {
2+ let num = n ;
3+ let output = 0 ;
4+ while ( num !== 1 ) {
5+ if ( num % 2 === 1 ) {
6+ output += 1 ;
7+ num = ( num - 1 ) / 2 ;
8+ } else {
9+ num = num / 2 ;
10+ }
11+ }
12+ if ( num === 1 ) {
13+ output += 1 ;
14+ }
15+ return output ;
16+ }
Original file line number Diff line number Diff line change 1+ function isPalindrome ( s : string ) : boolean {
2+ let start = 0 ;
3+ let last = s . length - 1 ;
4+ while ( start < last ) {
5+ while ( start < last && ! isAlphanumeric ( s . charAt ( start ) ) ) {
6+ start += 1 ;
7+ }
8+ while ( start < last && ! isAlphanumeric ( s . charAt ( last ) ) ) {
9+ last -= 1 ;
10+ }
11+ if ( s . charAt ( start ) . toLowerCase ( ) !== s . charAt ( last ) . toLowerCase ( ) ) {
12+ return false ;
13+ }
14+ start += 1 ;
15+ last -= 1 ;
16+ }
17+ return true ;
18+ }
19+
20+ function isAlphanumeric ( character : string ) {
21+ const characterCode = character . charCodeAt ( 0 ) ;
22+ const lowerStart = "a" . charCodeAt ( 0 ) ;
23+ const lowerEnd = "z" . charCodeAt ( 0 ) ;
24+ const upperStart = "A" . charCodeAt ( 0 ) ;
25+ const upperEnd = "Z" . charCodeAt ( 0 ) ;
26+ const numericStart = "0" . charCodeAt ( 0 ) ;
27+ const numericEnd = "9" . charCodeAt ( 0 ) ;
28+ if ( upperStart <= characterCode && upperEnd >= characterCode ) {
29+ return true ;
30+ }
31+ if ( lowerStart <= characterCode && lowerEnd >= characterCode ) {
32+ return true ;
33+ }
34+ if ( numericStart <= characterCode && numericEnd >= characterCode ) {
35+ return true ;
36+ }
37+ return false ;
38+ }
You can’t perform that action at this time.
0 commit comments