File tree Expand file tree Collapse file tree 1 file changed +6
-6
lines changed Expand file tree Collapse file tree 1 file changed +6
-6
lines changed Original file line number Diff line number Diff line change @@ -2,29 +2,29 @@ const combinationSum = function (candidates, target) {
2
2
const result = [ ] ;
3
3
const path = [ ] ;
4
4
5
- const dfs = function ( candidate , residual ) {
6
- if ( residual < 0 ) return ;
5
+ const dfs = function ( candidate , sum ) {
6
+ if ( sum > target ) return ;
7
7
8
8
path . push ( candidate ) ;
9
9
10
- if ( residual === 0 ) {
10
+ if ( sum === target ) {
11
11
result . push ( [ ...path ] ) ; //그냥 path 넣어주면 뒤에서 path 바뀔 때 result에 들어간 path도 같이 바뀜
12
12
path . pop ( ) ;
13
13
return ;
14
14
}
15
15
16
16
for ( let i = 0 ; i < candidates . length ; i ++ ) {
17
17
if ( candidates [ i ] >= candidate )
18
- //작은 애들 빼주는 건 이미 앞에서 했을 것이므로 큰 애들만 빼주면 됨
19
- dfs ( candidates [ i ] , residual - candidates [ i ] ) ;
18
+ //작은 애들 더해주는 건 이미 앞에서 했을 것이므로 큰 애들만 더해주면 됨
19
+ dfs ( candidates [ i ] , sum + candidates [ i ] ) ;
20
20
}
21
21
22
22
path . pop ( ) ;
23
23
} ;
24
24
25
25
for ( let i = 0 ; i < candidates . length ; i ++ ) {
26
26
//맨 처음에는 candidate이 없기 때문에 초기 조건 세팅할 때 candidate 넣어줘야 함
27
- dfs ( candidates [ i ] , target - candidates [ i ] ) ;
27
+ dfs ( candidates [ i ] , candidates [ i ] ) ;
28
28
}
29
29
30
30
return result ;
You can’t perform that action at this time.
0 commit comments