We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 8135236 commit 8769dc0Copy full SHA for 8769dc0
공예영/8주차/0039-combination-sum/0039-combination-sum.js
@@ -0,0 +1,30 @@
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
+ dfs(0,0,[]);
11
12
+ return ans;
13
14
+ function dfs(idx, sum, arr){
15
+ const current = sum + candidates[idx];
16
+ const currentArr = [...arr, candidates[idx]];
17
18
+ if(current > target) return;
19
20
+ if(current === target) {
21
+ ans.push(currentArr);
22
+ return;
23
+ };
24
25
+ dfs(idx, current,currentArr);
26
+ if(idx +1 < candidates.length) dfs(idx+1, sum, arr);
27
28
+ }
29
30
+};
0 commit comments