[Leetcode] (python): 039-combination Sum

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.