File tree Expand file tree Collapse file tree 5 files changed +103
-0
lines changed
product-of-array-except-self
validate-binary-search-tree Expand file tree Collapse file tree 5 files changed +103
-0
lines changed Original file line number Diff line number Diff line change
1
+ function threeSum ( nums : number [ ] ) : number [ ] [ ] {
2
+ nums . sort ( ( a : number , b : number ) => a - b ) ;
3
+ const results : [ number , number , number ] [ ] = [ ] ;
4
+
5
+ for ( let i = 0 ; i < nums . length - 2 ; i ++ ) {
6
+ if ( i > 0 && nums [ i ] === nums [ i - 1 ] ) continue ;
7
+
8
+ let left = i + 1 ;
9
+ let right = nums . length - 1 ;
10
+
11
+ while ( left < right ) {
12
+ const current = nums [ i ] + nums [ left ] + nums [ right ] ;
13
+ if ( current === 0 ) {
14
+ results . push ( [ nums [ i ] , nums [ left ] , nums [ right ] ] ) ;
15
+
16
+ while ( left < right && nums [ left ] === nums [ left + 1 ] ) left ++ ;
17
+ while ( left < right && nums [ right ] === nums [ right - 1 ] ) right -- ;
18
+
19
+ left ++ ;
20
+ right -- ;
21
+ } else if ( current < 0 ) {
22
+ left ++ ;
23
+ } else if ( current > 0 ) {
24
+ right -- ;
25
+ }
26
+ }
27
+ }
28
+
29
+ return results ;
30
+ }
Original file line number Diff line number Diff line change
1
+ function climbStairs ( n : number ) : number {
2
+ type Cache = { [ key : number ] : number } ;
3
+
4
+ const cache : Cache = {
5
+ 2 : 2 ,
6
+ 1 : 1 ,
7
+ } ;
8
+
9
+ const fibonacci = ( n : number , cache : Cache ) => {
10
+ if ( n in cache ) {
11
+ return cache [ n ] ;
12
+ } else {
13
+ cache [ n ] = fibonacci ( n - 2 , cache ) + fibonacci ( n - 1 , cache ) ;
14
+ return cache [ n ] ;
15
+ }
16
+ } ;
17
+
18
+ return fibonacci ( n , cache ) ;
19
+ }
Original file line number Diff line number Diff line change
1
+ function productExceptSelf ( nums : number [ ] ) : number [ ] {
2
+ const results = [ ] ;
3
+
4
+ let left = 1 ;
5
+ let right = 1 ;
6
+
7
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
8
+ results [ i ] = left ;
9
+ left *= nums [ i ] ;
10
+ }
11
+
12
+ for ( let i = nums . length - 1 ; i >= 0 ; i -- ) {
13
+ results [ i ] *= right ;
14
+ right *= nums [ i ] ;
15
+ }
16
+
17
+ return results ;
18
+ }
Original file line number Diff line number Diff line change
1
+ function isAnagram ( s : string , t : string ) : boolean {
2
+ if ( s . length !== t . length ) return false ;
3
+
4
+ const objS : { [ key : string ] : number } = { } ;
5
+
6
+ for ( const str of s ) {
7
+ objS [ str ] = ( objS [ str ] ?? 0 ) + 1 ;
8
+ }
9
+
10
+ for ( const str of t ) {
11
+ if ( ! objS [ str ] ) {
12
+ return false ;
13
+ }
14
+ objS [ str ] -- ;
15
+ }
16
+
17
+ return true ;
18
+ }
Original file line number Diff line number Diff line change
1
+ function isValidBST ( root : TreeNode | null ) : boolean {
2
+ return validateBSTHelper ( root , - Infinity , Infinity ) ;
3
+ }
4
+
5
+ const validateBSTHelper = (
6
+ root : TreeNode | null ,
7
+ minValue : number ,
8
+ maxValue : number
9
+ ) : boolean => {
10
+ if ( root === null ) {
11
+ return true ;
12
+ }
13
+ if ( root . val <= minValue || root . val >= maxValue ) {
14
+ return false ;
15
+ }
16
+ const leftIsValid = validateBSTHelper ( root . left , minValue , root . val ) ;
17
+ return leftIsValid && validateBSTHelper ( root . right , root . val , maxValue ) ;
18
+ } ;
You can’t perform that action at this time.
0 commit comments