leetcode刷题时遇到了78题Subset,写了上面的代码,在第15行最开始用了注释里的result.append(item),把item的引用(地址)传给了result,结果每次递归result中除了原有的‘[]’元素,其他都同步在变。
下面是nums=[1,2,3]时,使用result.append(item)语句的过程
item: [1] result: [[], [1]] item: [1, 2] result: [[], [1, 2], [1, 2]] item: [1, 2, 3] result: [[], [1, 2, 3], [1, 2, 3], [1, 2, 3]] item: [1, 3] result: [[], [1, 3], [1, 3], [1, 3], [1, 3]] item: [2] result: [[], [2], [2], [2], [2], [2]] item: [2, 3] result: [[], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3]] item: [3] result: [[], [3], [3], [3], [3], [3], [3], [3]]下面是深拷贝时的过程
item: [1] result: [[], [1]] item: [1, 2] result: [[], [1], [1, 2]] item: [1, 2, 3] result: [[], [1], [1, 2], [1, 2, 3]] item: [1, 3] result: [[], [1], [1, 2], [1, 2, 3], [1, 3]] item: [2] result: [[], [1], [1, 2], [1, 2, 3], [1, 3], [2]] item: [2, 3] result: [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3]] item: [3] result: [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
转载于:https://www.cnblogs.com/imageSet/p/8545307.html
相关资源:入门学习Linux常用必会60个命令实例详解doc/txt