Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.34 KB | None | 0 0
  1. def combination_sum(candidates, target)
  2.   tmp, res = [], []
  3.   sub_sum(candidates.sort!, tmp, target, res)
  4.   res
  5. end
  6.  
  7. def sub_sum(candidates, tmp, target, res)
  8.   return if target < 0
  9.   res << tmp.dup if target == 0
  10.  
  11.   candidates.each_with_index do |c, i|
  12.     sub_sum(candidates[i..-1], tmp << c, target - c, res)
  13.     tmp.pop
  14.   end
  15. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement