[leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
题意:
是的[leetcode]78. Subsets数组子集 follow up
这题强调给定数组可以有重复元素
思路:
需要先对给定数组排序,以便去重
代码:
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
if(nums == null || nums.length ==0 )return result;
Arrays.sort(nums);
List<Integer> path = new ArrayList<>();
dfs(0, nums, path, result);
return result;
} private void dfs(int index, int[] nums, List<Integer> path, List<List<Integer>> result){
result.add(new ArrayList<>(path));
for(int i = index; i < nums.length; i++){
if( i != index && nums[i] == nums[i-1] ) continue;
path.add(nums[i]);
dfs(i + 1, nums, path, result);
path.remove(path.size() - 1);
}
}
}
[leetcode]90. Subsets II数组子集(有重)的更多相关文章
- LeetCode 90. Subsets II (子集合之二)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- leetCode 90.Subsets II(子集II) 解题思路和方法
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [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, S, return all possible subsets. Note: ...
- Leetcode#90 Subsets II
原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- LeetCode Problem 90. Subsets II
python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
随机推荐
- windows openssh server 安装试用
使用Windows的可能会知道win10 的已经包好了openssh 服务,但是对于其他机器win 7 windows 2008 ,就需要其他的方法了 还好powershell 团队开发了支持wind ...
- java 从一个工程action 跳转到另外一个工程action
实现功能:java 从一个工程action 跳转到另外一个工程action 在我们实际编程的过程中,大家一定遇到过这种情况,那就是在一个工程中,需要使用到另外一个工程的实体Bean和方法.那么遇到这种 ...
- 6、 (★、※)root that results in a highest tree
问题:对于一棵特定的树,选择合适的根结点,使得树的高度最大. 思路: 先选择一个结点,从该结点开始遍历整棵树,获取能达到的最深的顶点(记为结点集合A): 然后从集合A中任意一个结点出发遍历整棵树,获取 ...
- OS&SYS&Shuti模块
#sys.argv 主要针对脚本可以读取参数 Shuti模块 import shutil f1=open('笔记',encoding='utf-8') f2=open('笔记2','w',enco ...
- 代码管理工具libgit2sharp与sharpsvn
在使用libgit2sharp 开发时出现: LibGit2Sharp.LibGit2SharpException: too many redirects or authentication repl ...
- 第四章 FFmpeg转码
4.1 FFmpeg软编码H.264与H.265 4.1.1 x264编码参数简介 4.1.2 H.264编码举例
- 利用SharpZipLib进行字符串的压缩和解压缩
http://www.izhangheng.com/sharpziplib-string-compression-decompression/ 今天搞了一晚上压缩和解压缩问题,java压缩的字符串,用 ...
- 执行webpack-dev-server时,提示端口被占用。
执行webpack-dev-server时总出错,提示端口被占用.百度了很多答案都不能解决,最后找到了解决方案,如下: webpack-dev-server --port 8088 使用以上命令修改 ...
- WPF 后台重写 DataTemplate
/// <summary> /// 配置类 /// </summary> public static class GridControlDeploy { /// <sum ...
- [java,2017-05-04] 创建word文档
package test; import java.text.SimpleDateFormat; import java.util.Date; import com.aspose.words.Data ...