码迷,mamicode.com
首页 > 其他好文 > 详细

040 Combination Sum II

时间:2015-07-18 07:02:33      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:

040 Combination Sum II 

这道题是 039 Combination Sum  的延伸, 稍作改动就好

class Solution:
    # @param {integer[]} candidates
    # @param {integer} target
    # @return {integer[][]}
    def combinationSum2(self, candidates, target):
        candidates.sort()
        ans = []
        self.help(candidates, target, [], -1, ans)
        return ans

    def help(self, a, target, cur, s, ans):
        if target == 0:
            ans.append(cur[:])
        if target < 0:
            return
        s += 1
        while s < len(a):
            cur.append(a[s])
            r = self.help(a, target - a[s], cur, s, ans)
            cur.pop()
            while s + 1 < len(a) and a[s+1] == a[s]:
                s += 1
            if target - a[s] < 0:
                break
            s += 1

 

040 Combination Sum II

标签:

原文地址:http://www.cnblogs.com/dapanshe/p/4656007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!