leetcode_组合和
typing import List
class Solution:
def combinationSum1(self, candidates: List[int], target: int) -> List[List[int]]:
#必须要排序 最后的结果可能会有顺序不一样但是元素一样的组合
candidates.sort()
res = []
reslist = []
成都创新互联自2013年创立以来,是专业互联网技术服务公司,拥有项目成都做网站、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元七星做网站,已为上家服务,为七星各地企业和个人服务,联系电话:18980820575
def dfs(candidates,target,reslist):
if target == 0 and reslist not in res:
res.append(reslist)
return
if target<0:
return
for i in range(len(candidates)):
print(reslist)
#这个递归太难,再理解
dfs(candidates[i+1:],target-candidates[i],reslist+[candidates[i]])
dfs(candidates,target,reslist)
function(){ //外汇点差 http://www.kaifx.cn/mt4/kaifx/1749.html
return res
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()
# for i in range(candidates):
res = []
reslist = []
def dfs(candidates,begin,target,reslist):
# for i in range(len(candidates)):
if target == 0 and reslist not in res:
res.append(reslist)
return
elif target<0 or begin>=len(candidates):
return
else:
# print(reslist,begin)
dfs(candidates,begin+1,target,reslist)
dfs(candidates,begin+1,target-candidates[begin],reslist+[candidates[begin]])
dfs(candidates,0,target,reslist)
return res
if name== "main":
a = Solution()
print(a.combinationSum1([2,3,6,7],7))
本文标题:leetcode_组合和
文章路径:http://pwwzsj.com/article/pehgoi.html