LintCode:数字组合
2016-09-03 16:47
155 查看
LintCode:数字组合
回溯算法,注意在最后要清除上一次的状态。
回溯算法,注意在最后要清除上一次的状态。
import copy class Solution: # @param candidates, a list of integers # @param target, integer # @return a list of lists of integers def combinationSum(self, candidates, target): # write your code here self.ans = [] candidates = sorted(list(set(candidates))) res = [] self.my_combinationSum(candidates, res, target, 0) return self.ans def my_combinationSum(self, candidates, res, target, tmp_sum): if tmp_sum > target: return if tmp_sum == target: if sorted(res) not in self.ans: self.ans.append(copy.copy(sorted(res))) return for index in range(0, len(candidates)): res.append(candidates[index]) tmp_sum += candidates[index] self.my_combinationSum(candidates, res, target, tmp_sum) tmp_sum -= candidates[index] res.pop()
相关文章推荐
- [Lintcode] #135 数字组合
- 数字组合-LintCode
- lintcode-153-数字组合 II
- LintCode 数字组合 题解
- lintcode-135-数字组合
- lintcode-落单的数III-84
- 最长上升连续子序列(LintCode)
- 删除链表中倒数第n个节点(LintCode)
- [TVYJ1096]数字组合
- [LintCode]Coins in a Line
- LintCode-爬楼梯
- lintcode: Sqrt(x)
- [LintCode] Expression Evaluation
- [LintCode] Remove Duplicates from Sorted List II
- LintCode-剑指Offer-(371)用递归打印数字
- LintCode:最大数
- LintCode_两个整数相除
- LintCode: Longest Common Substring
- LintCode 比较字符串
- 二分查找——LintCode