leetcode_ 组合和
更新:HHH   时间:2023-1-7


typing import List
class Solution:
def combinationSum1(self, candidates: List[int], target: int) -> List[List[int]]:
#必须要排序 最后的结果可能会有顺序不一样但是元素一样的组合
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))
返回web开发教程...