File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 87
87
| 203| [ 移除链表元素] ( https://leetcode.cn/problems/remove-linked-list-elements/ ) | [ JavaScript] ( ./algorithms/remove-linked-list-elements.js ) | Easy|
88
88
| 206| [ 反转链表] ( https://leetcode-cn.com/problems/reverse-linked-list/ ) | [ JavaScript] ( ./algorithms/reverse-linked-list.js ) | Easy|
89
89
| 209| [ 长度最小的子数组] ( https://leetcode.cn/problems/minimum-size-subarray-sum/ ) | [ JavaScript] ( ./algorithms/minimum-size-subarray-sum.js ) | Medium|
90
+ | 216| [ 组合总和 III] ( https://leetcode.cn/problems/combination-sum-iii/ ) | [ JavaScript] ( ./algorithms/combination-sum-iii.js ) | Medium|
90
91
| 222| [ 完全二叉树的节点个数] ( https://leetcode.cn/problems/count-complete-tree-nodes/ ) | [ JavaScript] ( ./algorithms/count-complete-tree-nodes.js ) | Medium|
91
92
| 225| [ 用队列实现栈] ( https://leetcode.cn/problems/implement-stack-using-queues/ ) | [ JavaScript] ( ./algorithms/implement-stack-using-queues.js ) | Easy|
92
93
| 226| [ 翻转二叉树] ( https://leetcode.cn/problems/invert-binary-tree/ ) | [ JavaScript] ( ./algorithms/invert-binary-tree.js ) | Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 216. 组合总和 III
3
+ * @param {number } k
4
+ * @param {number } n
5
+ * @return {number[][] }
6
+ */
7
+ var combinationSum3 = function ( k , n ) {
8
+ const result = [ ] ;
9
+ const path = [ ] ;
10
+ let sum = 0 ;
11
+
12
+ const backtracking = ( k , n , startIndex ) => {
13
+ if ( sum > n ) {
14
+ // 剪枝 ( 和已经大于 n 了, 继续往后遍历没有意义 )
15
+ return ;
16
+ }
17
+ if ( path . length === k ) {
18
+ if ( sum === n ) {
19
+ result . push ( path . slice ( ) ) ;
20
+ }
21
+ return ;
22
+ }
23
+ for ( let i = startIndex ; i <= 9 ; i ++ ) {
24
+ path . push ( i ) ;
25
+ sum += i ;
26
+ backtracking ( k , n , i + 1 , sum ) ;
27
+ sum -= i ;
28
+ path . pop ( ) ;
29
+ }
30
+ } ;
31
+ backtracking ( k , n , 1 ) ;
32
+
33
+ return result ;
34
+ } ;
You can’t perform that action at this time.
0 commit comments