List[List[int]]:#必須要排序 最后的結(jié)果可能會有順序..."/>
您好,登錄后才能下訂單哦!
typing import List
class Solution:
def combinationSum1(self, candidates: List[int], target: int) -> List[List[int]]:
#必須要排序 最后的結(jié)果可能會有順序不一樣但是元素一樣的組合
candidates.sort()
res = []
reslist = []
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))
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。