Leetcode 39: python可变类型复制(浅拷贝和深拷贝)
来源:互联网 发布:手机域名注册查询 编辑:程序博客网 时间:2024/06/06 09:43
刚刚在leetcode上写了一道算法题:39. Combination Sum,因为踩了python中复制的坑,花了很长时间才爬出来=。=
题目:
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited number of times.
起因是用python做完题后看了一下c++的解法:
class Solution {public: std::vector<std::vector<int> > combinationSum(std::vector<int> &candidates, int target) { std::sort(candidates.begin(), candidates.end()); std::vector<std::vector<int> > res; std::vector<int> combination; combinationSum(candidates, target, res, combination, 0); return res; }private: void combinationSum(std::vector<int> &candidates, int target, std::vector<std::vector<int> > &res, std::vector<int> &combination, int begin) { if (!target) { res.push_back(combination); return; } for (int i = begin; i != candidates.size() && target >= candidates[i]; ++i) { combination.push_back(candidates[i]); combinationSum(candidates, target - candidates[i], res, combination, i); combination.pop_back(); } }};
用了回溯的方法,非常完美啊有没有,于是想用python改写这个算法
class Solution(object): def combinationSum(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] """ def getComb(solution, candidates, index, target, res): if target < 0: return elif target == 0: res.append(solution) return for i in xrange(index, len(candidates)): if target < candidates[i]: break solution.append(candidates[i]) getComb(solution, candidates, i, target-candidates[i], res) solution.pop() candidates.sort() res = [] solution=[] getComb(solution, candidates, 0, target, res) return res
看起来是一模一样啊,可是得到的结果始终是:
[[],[]]
debug了好久,终于发现是这里出现了问题:
res.append(solution)
这里相当于将solution
直接复制到res
中,但是对象的引用并没有变,所以当执行
solution.append(candidates[i])
时,不仅会该改变solution
对象,同时也会改变res
,导致得不到想要的结果。
解决:
1.使用浅拷贝(因为这里的solution
类型为List[int]
,所以使用浅拷贝就OK啦):
s = copy.copy(solution)res.append(s)
2.不在函数中直接修改solution
,而是通过递归改变solution
的值:
class Solution(object): def combinationSum(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] """ def getComb(solution, candidates, index, target, res): if target < 0: return elif target == 0: res.append(solution) return for i in xrange(index, len(candidates)): if candidates[i] > target: break getComb(solution+[candidates[i]], candidates, i, target-candidates[i], res) candidates.sort() res = [] getComb([], candidates, 0, target, res) return res
这种写法还隐式地实现了回溯,真是很巧妙啊!
阅读全文
0 0
- Leetcode 39: python可变类型复制(浅拷贝和深拷贝)
- Python中的赋值以及深复制和浅复制(深拷贝和浅拷贝)
- python的复制,深拷贝和浅拷贝
- Python学习笔记——可变类型&不可变类型&深拷贝&浅拷贝
- python中的复制 浅拷贝 深拷贝
- python复制,浅拷贝,深拷贝理解
- python列表复制(浅拷贝and深拷贝)
- python字典复制(浅拷贝and深拷贝)
- 复制对象 和 浅拷贝,深拷贝
- Python 值类型 引用类型 和 深拷贝 浅拷贝 (九)
- python知识点(三):复制(引用、拷贝、浅拷贝和深拷贝)
- Python 深复制浅复制or深拷贝浅拷贝
- Python深复制浅复制or深拷贝浅拷贝
- Python深复制浅复制or深拷贝浅拷贝
- Python深复制浅复制or深拷贝浅拷贝
- Python深复制浅复制or深拷贝浅拷贝
- Python浅拷贝和和深拷贝
- python 的深拷贝和浅拷贝
- lfw数据验证
- mysql连接不到数据库
- ScalaHbase 使用scala 操作hbase
- BIO、NIO和AIO的区别
- Java初体验
- Leetcode 39: python可变类型复制(浅拷贝和深拷贝)
- 每日随笔_RenderTexture
- Bootstrap 学习网站
- ssh免密登录的一些坑
- Qt: 隐藏菜单QMenu
- MySQL日期函数大全
- 使用JNI写一个简单的程序
- 快速排序。冒泡排序递归和非递归的实现
- 浅谈C++多态&多态对象模型