Skip to content

Commit 8be06a0

Browse files
committed
combination-sum solution
1 parent 7b07157 commit 8be06a0

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* ๊ฐ๊ฐ ๋‹ค๋ฅธ ์ •์ˆ˜ ๋ฐฐ์—ด candidates์™€, ๋ชฉํ‘œ ์ •์ˆ˜ target์ด ์ฃผ์–ด์ง„๋‹ค.
3+
* ๋ฐ˜ํ™˜ํ•˜๋ผ, ํƒ€๊ฒŸ๊ณผ ๊ฐ’์ด ๊ฐ™์€ ํ•ฉ์„ ๋ณด์œ ํ•œ ๊ณ ์œ ํ•œ ์กฐํ•ฉ์„.
4+
* ์กฐํ•ฉ์€ ์–ด๋–ค ์ˆœ์„œ(any order)๋กœ๋“  ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
5+
* ๊ฐ™์€ ์ˆซ์ž๋ฅผ ์—ฌ๋Ÿฌ ์กฐํ•ฉ์— ๋ฌด์ œํ•œ(unlimited number of times) ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
6+
* ์„ ํƒํ•œ ์ˆซ์ž ์ค‘ ์ ์–ด๋„ ํ•˜๋‚˜์˜ ๋นˆ๋„๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด ๋‘ ์กฐํ•ฉ์€ ๊ณ ์œ ํ•ฉ๋‹ˆ๋‹ค.
7+
* target์€ 150๋ณด๋‹ค ๋‚ฎ์€ ์ˆซ์ž์ž…๋‹ˆ๋‹ค.
8+
*
9+
* @param {number[]} candidates
10+
* @param {number} target
11+
* @return {number[][]}
12+
*/
13+
var combinationSum = function(candidates, target) {
14+
var result = [];
15+
var nums = [];
16+
function dfs(start, total) {
17+
if (total > target) return;
18+
if (total === target) result.push(nums.slice());
19+
20+
21+
for (let i = start; i < candidates.length; i ++) {
22+
let num = candidates[i];
23+
nums.push(num);
24+
dfs(i, total + num);
25+
nums.pop();
26+
}
27+
28+
}
29+
dfs(0, 0);
30+
31+
return result;
32+
};

0 commit comments

Comments
ย (0)