鱼C论坛

 找回密码
 立即注册
查看: 1039|回复: 4

关于列表的问题

[复制链接]
发表于 2018-5-26 16:35:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在刷题时,有一个疑问不能解决,望大神们指教
题目要求 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用一次。
示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[
  [1, 7],
  [1, 2, 5],
  [2, 6],
  [1, 1, 6]
]
示例 2:

输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[
  [1,2,2],
  [5]
]
解:
class Solution:
    def dfs(self,candidates,start,target,path,ans):
        if target == 0:
            self.ans.append(path+[])
        for i in range(start,len(candidates)):
            if i > start and candidates[i] == candidates[i - 1]:
                continue
            if candidates[i] > target:
                break
            path.append(candidates[i])
            self.dfs(candidates,i+1,target-candidates[i],path,ans)
            path.pop()
        return ans

    def combinationSum2(self, candidates, target):

        candidates.sort()
        self.ans =[]
        self.dfs(candidates, 0, target,[], [])
        return self.ans
在 if target == 0:
            self.ans.append(path)这里为什么要path+[]? 不加[]返回的self.ans里所有元素都为[],这里不太明白,望大神们指点
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-5-27 13:25:46 | 显示全部楼层
本帖最后由 大地0725 于 2018-5-28 23:16 编辑
"""
给定一个数组candidates和一个目标数target,找出candidates中所有可以使数字和为target的组合。
candidates中的每个数字在每个组合中只能使用一次。

"""
import copy  
res = []  
class Solution:  
    def combinationSum(self, candidates, target):  
        """ 
        :type candidates: List[int] 
        :type target: int 
        :type: List[List[int]] 
        """  
        res.clear()  
        if len(candidates) == 0:  
            return res
        candidates.sort()  
        self.combinationSumHelper([],-1,target,candidates)  
        return res

    def combinationSumHelper(self,now,i,tar,candidates):
        if sum(now) == tar and now not in res:  
            dummy = copy.deepcopy(now)  
            res.append(dummy)  
            return  
        if sum(now) > tar:  
            return  
        else:  
            temp = -9999  
            for j in range(i+1,len(candidates)):  
                if candidates[j]!=temp:  
                    now.append(candidates[j])  
                    self.combinationSumHelper(now,j,tar,candidates)  
                    now.pop()  
                    temp = candidates[j]  
     
       
candidates = [10,1,-2,7,6,1,5]
target = 8
so= Solution()
print(so.combinationSum(candidates,target))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-5-27 16:15:44 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-5-28 23:33:25 | 显示全部楼层

看明白没?在python 3上运行符合你的意思和题意。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-5-31 21:10:02 | 显示全部楼层
大地0725 发表于 2018-5-28 23:33
看明白没?在python 3上运行符合你的意思和题意。

我知道怎么解答,我是想问+[]哪里为什么不加就有问题 list.append(path+[])这里,一般情况下这里不加[]和加[]是一样的,为什么这里会有问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-17 04:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表