Combination Sum II

来源:互联网 发布:手机淘宝账号交易平台 编辑:程序博客网 时间:2024/06/05 14:00

https://oj.leetcode.com/problems/combination-sum-ii/

据说阿里曾经出过这道题


Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inC where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … ,ak) must be in non-descending order. (ie, a1a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.
For example, given candidate set 10,1,2,7,6,1,5 and target 8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]


python实现

class Solution:    def searchOnce(self, candidates, target):        for i in xrange(len(candidates)):            if i > 0 and candidates[i] == candidates[i-1]:                continue                        if candidates[i] > target:                continue            elif candidates[i] < target:                self.tmp.append(candidates[i])                self.searchOnce(candidates[i+1:], target - candidates[i])                self.tmp.pop()            else:                lst = self.tmp[:]                lst.append(candidates[i])                lst.sort()                self.rslt.append(lst)                # @param candidates, a list of integers    # @param target, integer    # @return a list of lists of integers    def combinationSum2(self, candidates, target):        self.rslt = []        self.tmp = []        sorted_cd = sorted(candidates, reverse = True)        self.searchOnce(sorted_cd, target)        return self.rslt            if "__main__" == __name__:    s = Solution()    print s.combinationSum2([10,1,2,7,6,1,5], 8)    print s.combinationSum2([4,1,1,4,4,4,4,2,3,5], 10)

0 0
原创粉丝点击