We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents d9b73e8 + f4e0dcf commit 213809cCopy full SHA for 213809c
ruby/0040-combination-sum-ii.rb
@@ -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
26
+ backtrack([],0,target)
27
28
+ return @result
29
30
31
+end
0 commit comments