Java for LeetCode 090 Subsets II
Given a collection of integers that might contain duplicates, 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 = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
解题思路一:
偷懒做法,将Java for LeetCode 078 Subsets中的List换为Set即可通过测试,JAVA实现如下:
public List<List<Integer>> subsetsWithDup(int[] nums) {
Set<List<Integer>> list = new HashSet<List<Integer>>();
list.add(new ArrayList<Integer>());
Arrays.sort(nums);
for(int i=1;i<=nums.length;i++)
dfs(list, nums.length, i, 0,nums,-1);
return new ArrayList(list);
}
static List<Integer> alist = new ArrayList<Integer>();
static void dfs(Set<List<Integer>> list, int n, int k, int depth,int[] nums,int last) {
if (depth >= k) {
list.add(new ArrayList<Integer>(alist));
return;
}
for (int i = last+1; i <= n-k+depth; i++) {
alist.add(nums[i]);
dfs(list, n, k, depth + 1,nums,i);
alist.remove(alist.size() - 1);
}
}
解题思路二:
思路一其实用到了Java for LeetCode 077 Combinations和Java for LeetCode 078 Subsets的结论,使用set每次添加元素都需要查询一遍,会增加额外的时间开销,我们可以有一个不使用Set的解法,JAVA实现如下:
public List<List<Integer>> subsetsWithDup(int[] S) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> cur = new ArrayList<Integer>();
Arrays.sort(S);
dfs(0, res, cur, S);
return res;
}
private void dfs(int dep, List<List<Integer>> res, List<Integer> cur,
int[] S) {
if (dep == S.length) {
res.add(new ArrayList<Integer>(cur));
return;
}
int upper = dep;
while (upper >= 0 && upper < S.length - 1 && S[upper] == S[upper + 1])
upper++;
dfs(upper + 1, res, cur, S);
for (int i = dep; i <= upper; i++) {
cur.add(new Integer(S[dep]));
dfs(upper + 1, res, cur, S);
}
for (int i = dep; i <= upper; i++)
cur.remove(cur.size() - 1);
}
Java for LeetCode 090 Subsets II的更多相关文章
- [Leetcode Week8]Subsets II
Subsets II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets-ii/description/ Description Given ...
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [LeetCode]题解(python):090 Subsets II
题目来源 https://leetcode.com/problems/subsets-ii/ Given a collection of integers that might contain dup ...
- LeetCode 90. Subsets II (子集合之二)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- Java for LeetCode 078 Subsets
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
随机推荐
- Autolayout 03
Debugging in Code 有两个调试layout问题的阶段. 1. Map from “this view is in the wrong place” to “this constrain ...
- HTML小技巧将table边框改为细线
HTML制作新手在用TABLE表格时,会碰到如何改变边线粗线,因为默认的TABLE边线设置即使是1px 是很粗的.因此会使用其他一些方法来制作出细线边框,这里介绍一种利用CSS来实现细线的方法,很有效 ...
- php实现将人民币金额转大写的办法
class Num2Cny{ static $basical=array(0=>'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'); static $advance ...
- linux中expr用法
名称:expr ### 字串长度 shell>> expr length "this is a test" 14 ### 数字商数 shell>> ...
- 移动端去掉按钮button默认样式
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- [Sqlite]-->嵌入式数据库事务理解以及实例操作
引子: 1. Sqlite在Windows.Linux 和 Mac OS X 上的安装过程 2,嵌入式数据库的安装.建库.建表.更新表结构以及数据导入导出等等具体过程记录 SQLite 事务(Tran ...
- vscode 右键文件或者文件夹显示菜单
1.这个是可以在安装时直接选择显示的,如果跟我一样没有选也不愿意重新安装的,可以复制下面代码保存为vsCodeOpenFolder.reg,红色部分是vscode安装路径,换成自己本地路径即可. 双击 ...
- TCP/IP协议(数据封装与拆装过程)
IP地址 = 网络地址+主机地址 = 网络地址 + 子网地址 + 主机地址 应用进程之间的通信被称之为端到端的通信. 传输层与网络层之间的区别:传输层为应用进程间提供了端到端的逻辑通信:网络层提 ...
- HBase笔记
吴超 1.1 Hbase是Hadoop中的数据库,Hadoop还需要数据库吗?我们学的Hadoop是一个分布式的存储和计算的平台 为什么要在他上面建一个数据库呢,数据库是干什么的呢,数据库是一个管理系 ...
- JavaScript-4.1-简单的表单操作,函数用法---ShinePans
<html> <head> <meta http-equiv="content-type" content="text/html;chars ...