File tree 2 files changed +31
-0
lines changed
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 31
31
| 28| [ 实现 strStr()] ( https://leetcode-cn.com/problems/implement-strstr/ ) | [ JavaScript] ( ./algorithms/implement-strstr.js ) | Easy|
32
32
| 34| [ 在排序数组中查找元素的第一个和最后一个位置] ( https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/ ) | [ JavaScript] ( ./algorithms/find-first-and-last-position-of-element-in-sorted-array.js ) | Medium|
33
33
| 35| [ 搜索插入位置] ( https://leetcode.cn/problems/search-insert-position/ ) | [ JavaScript] ( ./algorithms/search-insert-position.js ) | Easy|
34
+ | 39| [ 组合总和] ( https://leetcode.cn/problems/combination-sum/ ) | [ JavaScript] ( ./algorithms/combination-sum.js ) | Medium|
34
35
| 48| [ 旋转图像] ( https://leetcode.cn/problems/rotate-image/ ) | [ JavaScript] ( ./algorithms/rotate-image.js ) | Medium|
35
36
| 49| [ 字母异位词分组] ( https://leetcode.cn/problems/group-anagrams/ ) | [ JavaScript] ( ./algorithms/group-anagrams.js ) | Medium|
36
37
| 54| [ 螺旋矩阵] ( https://leetcode.cn/problems/spiral-matrix/ ) | [ JavaScript] ( ./algorithms/spiral-matrix.js ) | Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 39. 组合总和
3
+ * @param {number[] } candidates
4
+ * @param {number } target
5
+ * @return {number[][] }
6
+ */
7
+ var combinationSum = function ( candidates , target ) {
8
+ const result = [ ] ;
9
+ const path = [ ] ;
10
+ let sum = 0 ;
11
+
12
+ const backtracking = ( candidates , target , startIndex ) => {
13
+ if ( sum > target ) return ;
14
+ if ( sum === target ) {
15
+ result . push ( path . slice ( ) ) ;
16
+ return ;
17
+ }
18
+ for ( let i = startIndex ; i < candidates . length ; i ++ ) {
19
+ let curr = candidates [ i ] ;
20
+ path . push ( curr ) ;
21
+ sum += curr ;
22
+ backtracking ( candidates , target , i ) ;
23
+ sum -= curr ;
24
+ path . pop ( ) ;
25
+ }
26
+ } ;
27
+ backtracking ( candidates , target , 0 ) ;
28
+
29
+ return result ;
30
+ } ;
You can’t perform that action at this time.
0 commit comments