Subsets II ——LeetCode
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],
[]
]
题目大意:给定一个数组,返回所有的可能子集合,数组有重复元素,但是子集合不能重复。
解题思路:这题我首先sort一下,然后遍历,当前元素不是重复的元素就把res里的所有的list取出来加上当前的元素再添加到res里,是重复元素就把上一次加入res的此元素list取出并加上重复元素再放入res。
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length == 0) {
return res;
}
Arrays.sort(nums);
res.add(new ArrayList<Integer>());
int lastSize = 0, currSize;
for (int i = 0; i < nums.length; i++) {
int start = (i > 0 && nums[i] == nums[i - 1]) ? lastSize : 0;
currSize = res.size();
for (int j = start; j < currSize; j++) {
List<Integer> tmp = new ArrayList<>(res.get(j));
tmp.add(nums[i]);
res.add(tmp);
}
lastSize = currSize;
}
return res;
}
Subsets II ——LeetCode的更多相关文章
- 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 ...
- [Leetcode Week8]Subsets II
Subsets II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets-ii/description/ Description Given ...
- Leetcode之回溯法专题-90. 子集 II(Subsets II)
Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- 【LeetCode】90.Subsets II
Subsets II Given a collection of integers that might contain duplicates, nums, return all possible s ...
- 90. Subsets II
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
随机推荐
- js自定义方法名
自定义方法名: <script language="javascript" type="text/javascript">window.onload ...
- sql语句中like的使用
先看一道题: 写出一条sql语句,找出表B中 字段Value中不全是字母 数字 下划线的数据 初看这道题,我们想到可以用like去进行模糊匹配,找出想要的结果.但是有一个地方需要注意:如果想在SQL ...
- NRPE: Unable to read output 问题处理总结
自定义nagios监控命令check_disk_data,首先在nagios服务端command.cfg定义了#'check_disk_data' command definitiondefine c ...
- linux常用命令之ln
ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同不的链接,这个命令最常用的参数是-s,具体用法是:ln –s 源文件 目标文件. 当我们需要在不同的目录,用到相同的 ...
- 九、C# 合式类型
本章要描述如何最终完善类型声明. 1.重写Ojbect中的成员 重写ToString() 默认情况下,在任何对象上调用 ToString()会返回类的完全限定名称,所以有时候需要重载这个函数, ...
- SGU 111.Very simple problem
题目大意: 求平方不大于n(n<=10^1000)的最大的数. 分析: 二分+高精度乘法 或者 高精度开方... ...
- SGU 143.Long Live the Queen(女王万岁)
时间限制:0.25s 空间限制:4M 题意: 有n(n<=16000)个小镇,每两个小镇有且仅有一条路径相连.每个小镇有一个收益x(-1000<=x<=1000). 现在要求,选择一 ...
- 【POJ2752】【KMP】Seek the Name, Seek the Fame
Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...
- Windows的命令行怎么支持通配符
摸索出一个小技巧,虽然Windows的命令行本身不支持通配符,但可以在脚本里把传进来的参数当通配符用 只要加上@ARGV = glob "@ARGV";就行了 @ARGV = gl ...
- Mac使用rz、sz远程上传下载文件
习惯了 在windows下的securecrt和xshell的rz 和sz. rz 可以很方便的从客户端传文件到服务器,sz也可以很方便的从服务器传文件到客户端,就算中间隔着跳板机也不影响.在mac下 ...