File tree Expand file tree Collapse file tree 4 files changed +66
-0
lines changed
find-minimum-in-rotated-sorted-array
maximum-depth-of-binary-tree Expand file tree Collapse file tree 4 files changed +66
-0
lines changed Original file line number Diff line number Diff line change
1
+ function combinationSum ( candidates : number [ ] , target : number ) : number [ ] [ ] {
2
+ const result : number [ ] [ ] = [ ] ;
3
+
4
+ const dfs = ( start : number , path : number [ ] , sum : number ) => {
5
+ if ( sum === target ) {
6
+ result . push ( [ ...path ] ) ;
7
+ return ;
8
+ }
9
+
10
+ if ( sum > target ) {
11
+ return ;
12
+ }
13
+
14
+ for ( let i = start ; i < candidates . length ; i ++ ) {
15
+ path . push ( candidates [ i ] ) ;
16
+ dfs ( i , path , sum + candidates [ i ] ) ;
17
+ path . pop ( ) ;
18
+ }
19
+ } ;
20
+
21
+ dfs ( 0 , [ ] , 0 ) ;
22
+ return result ;
23
+ }
Original file line number Diff line number Diff line change
1
+ function findMin ( nums : number [ ] ) : number {
2
+ let left = 0 ,
3
+ right = nums . length - 1 ;
4
+
5
+ while ( left < right ) {
6
+ const mid = left + Math . floor ( ( right - left ) / 2 ) ;
7
+ if ( nums [ mid ] > nums [ right ] ) {
8
+ left = mid + 1 ;
9
+ } else {
10
+ right = mid ;
11
+ }
12
+ }
13
+
14
+ return nums [ left ] ;
15
+ }
Original file line number Diff line number Diff line change
1
+ function maxDepth ( root : TreeNode | null ) : number {
2
+ if ( ! root ) return 0 ;
3
+ const left = maxDepth ( root . left ) ;
4
+ const right = maxDepth ( root . right ) ;
5
+ return Math . max ( left , right ) + 1 ;
6
+ }
Original file line number Diff line number Diff line change
1
+ function mergeTwoLists (
2
+ list1 : ListNode | null ,
3
+ list2 : ListNode | null
4
+ ) : ListNode | null {
5
+ const result = new ListNode ( ) ;
6
+ let tail = result ;
7
+
8
+ while ( list1 !== null && list2 !== null ) {
9
+ if ( list1 . val <= list2 . val ) {
10
+ tail . next = list1 ;
11
+ list1 = list1 . next ;
12
+ } else {
13
+ tail . next = list2 ;
14
+ list2 = list2 . next ;
15
+ }
16
+ tail = tail . next ;
17
+ }
18
+
19
+ tail . next = list1 !== null ? list1 : list2 ;
20
+
21
+ return result . next ;
22
+ }
You can’t perform that action at this time.
0 commit comments