We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent f2ffb4a commit a4ce226Copy full SHA for a4ce226
공예영/7주차/0039-combination-sum/0039-combination-sum.js
@@ -0,0 +1,29 @@
1
+/**
2
+ * @param {number[]} candidates
3
+ * @param {number} target
4
+ * @return {number[][]}
5
+ */
6
+var combinationSum = function(candidates, target) {
7
+ const ans = [];
8
+ candidates.sort((a, b) => a - b);
9
+
10
+ function dfs(idx, sum, arr) {
11
+ if (sum === target) {
12
+ ans.push([...arr]);
13
+ return;
14
+ }
15
16
+ if (sum > target || idx >= candidates.length) {
17
18
19
20
+ arr.push(candidates[idx]);
21
+ dfs(idx, sum + candidates[idx], arr);
22
23
+ arr.pop();
24
+ dfs(idx + 1, sum, arr);
25
26
27
+ dfs(0, 0, []);
28
+ return ans;
29
+};
0 commit comments