Subsets
来源:互联网 发布:linux 打开vim编辑器 编辑:程序博客网 时间:2024/06/05 20:33
题目描述:
分析:递归计算
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]
分析:递归计算
代码:
class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> result = subsetsHelper(nums, 0); result.add(new ArrayList<Integer>()); return result; } private List<List<Integer>> subsetsHelper(int[] nums, int index) { if (index == nums.length) { return new ArrayList<>(); } List<List<Integer>> result = new ArrayList<>(); List<List<Integer>> subresult = subsetsHelper(nums, index + 1); for (List<Integer> l : subresult) { result.add(new ArrayList<>(l)); l.add(nums[index]); } List<Integer> single = new ArrayList<>(); single.add(nums[index]); subresult.add(single); result.addAll(subresult); return result; }}
阅读全文
0 0
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- Subsets
- jQuery语法
- PHP生成加减算法方式的验证码
- 使用@property 改变get和set
- Spring对注解(Annotation)处理源码分析1——扫描和读取Bean定义
- 15.1 报文与实体
- Subsets
- MySQL分组数据
- 排序算法
- Spring基础_在JavaConfig中引用xml配置<四>
- MySQL索引有啥好处!怎么合理的添加
- Java配置----JDK开发环境搭建及环境变量配置
- yum 升级php版本
- Android的发展历史
- 遇到的那些可爱的面试题2017-10-30