File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } candidates
3
+ * @param {number } target
4
+ * @return {number[][] }
5
+ */
6
+ var combinationSum = function ( candidates , target ) {
7
+ const result = [ ]
8
+ function backtrack ( start , curr , sum ) {
9
+ // ํ์ฌ ํฉ๊ณ๊ฐ ํ๊ฒ๊ณผ ๊ฐ์ผ๋ฉด ๊ฒฐ๊ณผ์ ์ถ๊ฐ
10
+ if ( sum === target ) {
11
+ result . push ( [ ...curr ] )
12
+ }
13
+
14
+ // ํฉ๊ณ๊ฐ ํ๊ฒ์ ์ด๊ณผํ๋ฉด ๋ ์ด์ ์งํํ์ง ์์
15
+ if ( sum > target ) {
16
+ return
17
+ }
18
+
19
+ // ํ์ฌ ์ธ๋ฑ์ค๋ถํฐ ์์ํ์ฌ ๋ชจ๋ ํ๋ณด๋ฅผ ์๋
20
+ for ( let i = start ; i < candidates . length ; i ++ ) {
21
+ curr . push ( candidates [ i ] )
22
+ // ๊ฐ์ ์ซ์๋ฅผ ์ฌ๋ฌ ๋ฒ ์ฌ์ฉํ ์ ์์ผ๋ฏ๋ก i๋ถํฐ ๋ค์ ์์
23
+ backtrack ( i , curr , sum + candidates [ i ] )
24
+ curr . pop ( )
25
+ }
26
+ }
27
+ backtrack ( 0 , [ ] , 0 )
28
+ return result
29
+ } ;
You canโt perform that action at this time.
0 commit comments