Subsets
来源:互联网 发布:移动直播app 开源 源码 编辑:程序博客网 时间:2024/06/06 07:15
Given a set of distinct integers, S, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
For example,
If S = [1,2,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]
Analysis:
There are two ways to think about it.
first: each element in S could be selected or not selected. we can use recursion.
second: for n.th element, the number of subsets is twice as large as that of subsets created by first n-1 elements. we can use recursion or iteration.
class Solution: # @param S, a list of integer # @return a list of lists of integer ## wu ## recursive def subsets(self, S): if len(S)==0: return [[]] res = [[]] S = sorted(S) for elem in S: newres = [] for elemres in res: new = elemres[:] new.append(elem) newres.append(new) res = res + newresHTere return res
Summary:
get the deeper understanding of recursion and iteration. For iteration, it is usual that there is a for loop in it. For recursion, we only have one function to enter the process and another function will call itself many times. Sometimes, there is a internal variable pass through all iterative funcitons.
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Linux下DedeCMS详细安全设置教程
- 【hash_map】hash_map中键为自定义类型的操作
- activiti5第二弹----使用activiti5提供的测试类进行测试
- 运行Titan下的gremlin.sh出现VerifyError的解决办法
- hiho #1066并查集问题
- Subsets
- 为SUSE Linux添加软件安装源
- StringBuilder学习笔记
- 将整形的二进制数翻转再求值
- 黑马程序员_Java反射机制学习笔记
- Oracle计算时间差函数
- Ehcache配置参数简介
- Poj 1163 题解
- 【转载】【OpenCV】访问Mat中每个像素的值(新)