[LeetCode]Subsets
2016-04-27 10:45
411 查看
Given a set of distinct integers, nums, 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 nums =
is:
Subscribe to see which companies asked this question
题解:使用递归
code:
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<List<Integer>>();
List<Integer> res = new ArrayList<Integer>();
sovle(0, nums.length, nums, res, ans);
return ans;
}
private void sovle(int cur ,int length, int[] nums, List<Integer> res, List<List<Integer>> ans)
{
ans.add(new ArrayList<Integer>(res));
for(int i = cur; i<length; i++)
{
res.add(nums[i]);
sovle(i+1 ,length, nums, res, ans);
res.remove(res.size() - 1);
}
}
}
Note:
Elements in a subset must be in non-descending order.
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], [] ]
Subscribe to see which companies asked this question
题解:使用递归
code:
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<List<Integer>>();
List<Integer> res = new ArrayList<Integer>();
sovle(0, nums.length, nums, res, ans);
return ans;
}
private void sovle(int cur ,int length, int[] nums, List<Integer> res, List<List<Integer>> ans)
{
ans.add(new ArrayList<Integer>(res));
for(int i = cur; i<length; i++)
{
res.add(nums[i]);
sovle(i+1 ,length, nums, res, ans);
res.remove(res.size() - 1);
}
}
}
相关文章推荐
- Ntddk.h 和 Ntifs.h同时包含时的重复定义问题
- JSP页面元素构成
- 内存对齐的malloc、realloc、free
- (4.6.11.4)加速Android Studio的Gradle构建速度
- iOS点击屏幕任意位置, 隐藏键盘(手势)
- 基于gtid复制模式下的主从同步搭建测试
- 解决oracle使用rownum排序分页排序字段不唯一导致分页错误
- 正则表达式 分组命名 替换
- 软件架构设计(摘抄)
- [SharePoint 2013] Automatic deployment script
- 了解android系统
- Sublime Text2.0.2注册码
- 手机页面分页加载更多
- Oracle创建用户、表空间并设置权限
- 站立会议9
- 音乐波形数组
- JAVA 判断一个整数是否为2的整数次幂的方法
- 为什么红黑树的效率比较高
- iOS9的调整
- Android官方侧滑菜单DrawerLayout类似slidingMenu