鱼C论坛

 找回密码
 立即注册
查看: 1021|回复: 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 编辑

  1. """
  2. 给定一个数组candidates和一个目标数target,找出candidates中所有可以使数字和为target的组合。
  3. candidates中的每个数字在每个组合中只能使用一次。

  4. """
  5. import copy  
  6. res = []  
  7. class Solution:  
  8.     def combinationSum(self, candidates, target):  
  9.         """
  10.         :type candidates: List[int]
  11.         :type target: int
  12.         :type: List[List[int]]
  13.         """  
  14.         res.clear()  
  15.         if len(candidates) == 0:  
  16.             return res
  17.         candidates.sort()  
  18.         self.combinationSumHelper([],-1,target,candidates)  
  19.         return res

  20.     def combinationSumHelper(self,now,i,tar,candidates):
  21.         if sum(now) == tar and now not in res:  
  22.             dummy = copy.deepcopy(now)  
  23.             res.append(dummy)  
  24.             return  
  25.         if sum(now) > tar:  
  26.             return  
  27.         else:  
  28.             temp = -9999  
  29.             for j in range(i+1,len(candidates)):  
  30.                 if candidates[j]!=temp:  
  31.                     now.append(candidates[j])  
  32.                     self.combinationSumHelper(now,j,tar,candidates)  
  33.                     now.pop()  
  34.                     temp = candidates[j]  
  35.      
  36.       
  37. candidates = [10,1,-2,7,6,1,5]
  38. target = 8
  39. so= Solution()
  40. 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-5-4 15:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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