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. ...
随机推荐
- Java使用poi对Execl简单_读_操作
public class ReadExecl { // private final String XLSX = ".xlsx"; // 2007以上版本 // private fi ...
- JavaScript核心
JavaScript核心 arguments对象 Array对象 Boolean对象 Date对象 Error对象 Function对象 Global对象 Math对象 Number对象 Object ...
- Win10安卓模拟器Visual Studio Emulator for Android使用简介(转)
Visual Studio Emulator for Android是微软官方发布的独立版本的安卓模拟器,这款软件可以让安卓应用开发者更加轻松的用Visual Studio编写Android应用,据说 ...
- AppStore上架规则
1. 条款和条件1.1 为App Store开发程序,开发者必须遵守 Program License Agreement (PLA).人机交互指南(HIG)以及开发者和苹果签订的任何协议和合同.以下规 ...
- AFN的坑--NSCachedURLResponse缓存
网络正常的情况下,如果服务器宕机或者数据库出错,会造成访问服务器报错的情况,一般报错的内容是:无法连接到服务器或者其它错误.且服务器 修复后,仍然报错.经过排查,终于找出了原因所在:AFNetwork ...
- 嵌入式css样式,写在当前的文件中
现在有一任务,把下面的“超酷的互联网”.“服务及时贴心”.“有趣易学”这三个短词文字字号修改为18px. 如果用内联式css样式的方法进行设置将是一件很头疼的事情(为每一个<span>标签 ...
- C#入门经典(第五版)学习笔记(三)
---------------面向对象编程简介--------------- UML表示方法: 1)方框上中下三分 2)上框写类名 3)中框写属性和字段,例如:+Description:string ...
- yum安装ftp服务器
1.安装vsftp,本文采用yum安装: #yum install vsftpd 2.安装后运行: # service vsftpd restart Shutting downvsftpd: ...
- LGDT/LIDT-加载全局/中断描述符表寄存器
将源操作数中的值加载到全局描述符表寄存器 (GDTR) 或中断描述符表寄存器 (IDTR).源操作数指定 6 字节内存位置,它包含全局描述符表 (GDT) 或中断描述符表 (IDT) 的基址(线性地址 ...
- WPF自定义DataGrid分页控件
新建Custom Control,名:PagingDataGrid 打开工程下面的Themes\Generic.xaml xaml里面代码替换如下 <Style x:Key="{x:T ...