[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 ...
随机推荐
- Linux系统入门学习:在curl中设置自定义的HTTP头
http://www.linuxidc.com/Linux/2015-02/114220.htm
- MVC中数据的内部校验
针对MVC中实例类型的修饰,我们可以采用DataAnnotations类来完成,该类所处的命名空间是System.ComponentModel.DataAnnotations; 一.通过开类型,能够修 ...
- 攻城狮在路上(贰) Spring(二)--- Spring IoC概念介绍
一.IoC的概念: IoC(控制反转)是Spring容器的核心.另一种解释是DI(依赖注入),即让调用类对某一个接口的依赖关系由第三方注入,以移除调用类对某一个接口实现类的一览. 定义如此,由此可见, ...
- hdu 5033 单调栈 ****
看出来是单调栈维护斜率,但是不会写,2333,原来是和询问放在一起的 #include <iostream> #include <cstdio> #include <cs ...
- 安装wine的问题
- Linux SSH远程文件/目录传输命令scp
转载地址:http://www.vpser.net/manage/scp.html 相信各位VPSer在使用VPS时会经常在不同VPS间互相备份数据或者转移数据,大部分情况下VPS上都已经安装了Ngi ...
- C# jsonhelper
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Sc ...
- sublime总结
自定义快捷键: preferences->key binding->user ctrl+d 删除行 ctrl+k 选中下一同名变量,alt+F3 选中全部同名变量 [ {"key ...
- 安装.net Framework 3.5 SP1非常慢的解决方案
解决方案:1.msiexec /unregserver回车,在输入命令:msiexec /regserver msiexec /unregserver是停止installer服务,而msiexec / ...
- ios录音
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewCont ...