网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

对pythonappend与浅拷贝的实例讲解-创新互联

在做Leetcode的第39题的时候,看到网上一个用递归的解法,很简洁。于是重写了一遍。

网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、微信小程序、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了保山免费建站欢迎大家使用!
class Solution(object):
 def combinationSum(self, candidates, target):
 """
 :type candidates: List[int]
 :type target: int
 :rtype: List[List[int]]
 """
 result,temp = [],[]
 self.combinationSumRecu(sorted(candidates),result,0,temp,target)
 return result

 def combinationSumRecu(self, candidates, result, start, temp, target):
 if target == 0:
  result.append(temp) # 注意此处不能直接append(temp),否则是浅拷贝,之后temp.pop()时会将result中的数也pop出来
 while start < len(candidates) and candidates[start]<=target:
  temp.append(candidates[start])
  self.combinationSumRecu(candidates, result, start, temp,target-candidates[start])
  temp.pop()
  start += 1

if __name__ == '__main__':
 print Solution().combinationSum([2,3,6,7],7)

分享名称:对pythonappend与浅拷贝的实例讲解-创新互联
当前路径:http://bjjierui.cn/article/diopjo.html

其他资讯