[Leetcode] Subsets II
Given a collection of integers that might contain duplicates, 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,2]
, a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
Solution:
public class Solution {
public List<List<Integer>> subsetsWithDup(int[] num) {
Arrays.sort(num);
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> al = new ArrayList<Integer>();
dfs(result, al, num, 0);
return result;
} private void dfs(List<List<Integer>> result, List<Integer> al, int[] s,
int start) {
// TODO Auto-generated method stub
result.add(new ArrayList<Integer>(al));
for (int i = start; i < s.length; ++i) {
al.add(s[i]);
dfs(result, al, s, i+1);
al.remove(al.size()-1);
while((i+1<s.length)&&(s[i]==s[i+1]))
++i;
}
}
}
[Leetcode] Subsets II的更多相关文章
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- [LeetCode] Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [leetcode]Subsets II @ Python
原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might cont ...
- [LeetCode] Subsets II [32]
题目 Given a collection of integers that might contain duplicates, S, return all possible subsets. Not ...
- [Leetcode] subsets ii 求数组所有的子集
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [LeetCode]Subsets II生成组合序列
class Solution {//生成全部[不反复]的组合.生成组合仅仅要採用递归,由序列从前往后遍历就可以. 至于去重,依据分析相应的递归树可知.同一个父节点出来的两个分支不能一样(即不能与前一个 ...
- LeetCode:Subsets I II
求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...
- Subsets II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Subsets II - LeetCode 注意点 有重复的数字 数组可能是无序的,要先排序 解法 解法一:递归,只需要在Subsets中递归写法的基础上 ...
- LeetCode解题报告—— Word Search & Subsets II & Decode Ways
1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...
随机推荐
- .net学习笔记----WebConfig常用配置节点介绍
一.配置文件入门 .Net提供了一种保存项目配置信息的办法,就是利用配置文件,配置文件的后缀一般是.config.在WinForm程序中配置文件一般是App.config.在Asp.net中一般默认是 ...
- ASP.NET MVC中ViewData、ViewBag和TempData
1.ViewData 1.1 ViewData继承了IDictionary<string, object>,因此在设置ViewData属性时,传入key必须要字符串型别,value可以是任 ...
- 【转载】python super的用法
转载地址: http://blog.csdn.net/cxm19830125/article/details/20610533 super的用法是调用继承类的初始化方法,如下面的代码: class A ...
- 编程风格规范google版
python's coding style,google 命名
- MDX语法之排序函数Order
使用场景: 排列指定集的成员,可以选择保留或打乱原有的层次结构. 语法: Numeric expression syntax Order(Set_Expression, Numeric_Express ...
- android 面试题
一,什么是OOM (1)先从定义开始:Android(Java)中常见的容易引起内存泄漏的不良代码Android主要应用在嵌入式设备当中,而嵌入式设备由于一些众所周知的条件限制,通常都不会有很高的配置 ...
- bbed的使用--安装及初探
bbed是oracle内部一款用来直接查看和修改数据文件数据的工具,可以直接修改Oracle数据文件块的内容,在一些特殊恢复场景下比较有用. 1.bbed 的安装 在9i/10g中连接生成bbed: ...
- Sp EF输出 临时表
-- ============================================= -- Author: <Author,,Name> -- Create date: < ...
- PC端重置
-PC 一,meta <!DOCTYPE html> <html lang="zh-cmn-Hans"> <head> <meta cha ...
- 在Android上用AChartEngine轻松绘制图表
本文由 伯乐在线 - LeonHover 翻译.未经许可,禁止转载!英文出处:jaxenter.欢迎加入翻译组. Android发布不久的2008年底,开发者们已经开始寻找制表.制图.绘图的工具库.当 ...