DAY19:leetcode #39 Combination Sum
来源:互联网 发布:南昌网络教育 编辑:程序博客网 时间:2024/06/07 18:02
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 may be chosen from C unlimited number of times.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7]
and target 7
,
A solution set is:
[ [7], [2, 2, 3]]
Subscribe to see which companies asked this question
import copyclass Solution(object): def combinationSum(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] """ tree = [] result = set() candidates_len = len(candidates) candidates.sort() while(sum(tree) < target): tree.append(candidates[0]) while(True): if sum(tree) == target: #print tree result.add(tuple(sorted(copy.deepcopy(tree)))) #print result pos = candidates_len flag = True while pos == candidates_len: del tree[-1] if len(tree) == 0: flag = False break pos = candidates.index(tree[-1]) + 1 if not flag: break tree[-1] = candidates[candidates.index(tree[-1])+1] elif sum(tree) > target: pos = candidates_len flag = True while pos == candidates_len: del tree[-1] if len(tree) == 0: flag = False break pos = candidates.index(tree[-1]) + 1 if not flag: break tree[-1] = candidates[candidates.index(tree[-1])+1] else: while(sum(tree) < target): tree.append(candidates[0]) pos = candidates.index(tree[-1]) + 1 flag = True while pos == candidates_len: del tree[-1] if len(tree) == 0: flag = False break pos = candidates.index(tree[-1]) + 1 if not flag: break tree[-1] = candidates[candidates.index(tree[-1])+1] result = list(result) for i in range(len(result)): result[i] = list(result[i]) return result
0 0
- DAY19:leetcode #39 Combination Sum
- DAY19:leetcode #40 Combination Sum II
- LeetCode 39: Combination Sum
- LeetCode(39) Combination Sum
- [leetcode 39] Combination Sum
- leetcode 39: Combination Sum
- leetcode || 39、Combination Sum
- leetcode 39 : Combination Sum
- leetcode 39: Combination Sum
- Leetcode #39 Combination Sum
- LeetCode(39) Combination Sum
- LeetCode-39 Combination Sum
- leetcode 39:Combination Sum
- LeetCode 39: Combination Sum
- Leetcode 39 - Combination Sum
- 【leetcode】【39】Combination Sum
- LeetCode 39 - Combination Sum
- LeetCode(39)-Combination Sum
- 安装cocoapods遇到的问题总结
- 十一月基本规划
- 接口隔离原则(ISP - Interface Segregation Principle)
- 谈谈推荐系统(一)什么是推荐系统
- IntelliJ IDEA 注册码(phpstorm等IDE)
- DAY19:leetcode #39 Combination Sum
- 大师带你了解TCP基本功之滑动窗口(1)
- sql语句的打印机获取字段
- mathNet基础用法
- 解决手机浏览器和微信中select中border:none;无用和去掉小三角
- 自定义一个进度条为圆角的progressbar
- Java多线程同步(锁)的实现方法(synchronised 与reentrantlock)
- setContentView
- 博客流量量2K人次标记