Source of the topic
https://leetcode.com/problems/combination-sum/
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C Where the candidate numbers sums to T.
The same repeated number is chosen from C unlimited number of times.
Test instructions Analysis
Input:a list as candidates, a value named target
Output:the list number, sumed to target
Conditions: Find a number in the list, make and for target, note that each number can be taken several times
Note:
- All numbers (including target) would be positive integers.
- Elements in a combination (a1, a 2, ..., aK) must is in non-descending order. (ie, a1≤ a2≤ ... ≤ ak).
- The solution set must not contain duplicate combinations.
For example, given candidate set and 2,3,6,7
target 7
,
A Solution set is:
[7]
[2, 2, 3]
Topic ideas
Sort the list first, and then do a poor lift
AC Code (PYTHON)
1_author_ ="YE"2 #-*-coding:utf-8-*-3 4 classsolution (object):5 deffind (self,candidates, Target, start, valueList):6 iftarget = =0:7 Solution.ans.append (valueList)8Length =Len (candidates)9 forIinchRange (start, length):Ten ifCandidates[i] >Target: One return ASelf.find (candidates, target-candidates[i], I, ValueList +[Candidates[i]]) - - defcombinationsum (self, candidates, target): the """ - : Type Candidates:list[int] - : Type Target:int - : Rtype:list[list[int]] + """ - Candidates.sort () +Solution.ans = [] A Self.find (candidates, target, 0, []) at returnSolution.ans - -s =solution () -Candidates = [2,3,6,7] -target = 7 - Print(S.combinationsum (candidates, target))
[Leetcode] (python): 039-combination Sum