typing import List
class Solution:
def combinationSum1(self, candidates: List[int], target: int) -> List[List[int]]:
#必須要排序 最后的結(jié)果可能會有順序不一樣但是元素一樣的組合
candidates.sort()
res = []
reslist = []
成都創(chuàng)新互聯(lián)自2013年創(chuàng)立以來,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都做網(wǎng)站、網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元七星做網(wǎng)站,已為上家服務(wù),為七星各地企業(yè)和個人服務(wù),聯(lián)系電話: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))