Skip to content

Commit 213809c

Browse files
authored
Merge pull request #2078 from saip7795/sp/combination-sum-ii
Create: 0040-Combination-Sum-II.rb
2 parents d9b73e8 + f4e0dcf commit 213809c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Diff for: ruby/0040-combination-sum-ii.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def combination_sum2(candidates, target)
2+
@candidates = candidates
3+
@candidates.sort!
4+
@result = []
5+
6+
def backtrack (cur,pos,target)
7+
@result.append(cur.dup()) if target == 0
8+
return if target <=0
9+
10+
prev = -1
11+
12+
(pos..@candidates.length-1).each do |i|
13+
next if @candidates[i] == prev
14+
15+
cur.append(@candidates[i])
16+
17+
backtrack(cur,i+1,target-@candidates[i])
18+
19+
cur.pop()
20+
21+
prev = @candidates[i]
22+
23+
end
24+
25+
end
26+
backtrack([],0,target)
27+
28+
return @result
29+
30+
31+
end

0 commit comments

Comments
 (0)