-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path040.rb
More file actions
27 lines (24 loc) · 664 Bytes
/
040.rb
File metadata and controls
27 lines (24 loc) · 664 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def find_result(candidates, i, target, results, list)
if target == 0
results.push(list.dup)
return
end
return if target < 0
return if i == candidates.length
i.upto(candidates.length - 1).each do |index|
next if index > i && candidates[index] == candidates[index - 1]
list.push(candidates[index])
find_result(candidates, index + 1, target - candidates[index], results, list)
list.pop
end
end
# @param {Integer[]} candidates
# @param {Integer} target
# @return {Integer[][]}
def combination_sum2(candidates, target)
candidates.sort!
results = []
list = []
find_result(candidates, 0, target, results, list)
results
end