GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — algorithms/backtrack/combination_sum.py at master · vspriya05/algorithms Expand file tree/ Copy pathcombination_sum.pyMore file actions 31 lines (27 loc) · 864 Bytes / Copy pathcombination_sum.pyFile metadata and controls31 lines (27 loc) · 864 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 28 29 30 31 """ Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. For example, given candidate set [2, 3, 6, 7] and target 7, A solution set is: [ [7], [2, 2, 3] ] """ def combinationSum(self, candidates, target): res = [] candidates.sort() self.dfs(candidates, target, 0, [], res) return res def dfs(self, nums, target, index, path, res): if target < 0: return # backtracking if target == 0: res.append(path) return for i in range(index, len(nums)): self.dfs(nums, target-nums[i], i, path+[nums[i]], res) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |