90. 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],
- []
- ]
题解:
Subsets II, 关键是去重复。我们依然使用跟其他dfs +backtracking类似的方法, 当i > pos && nums[i] == nums[i - 1]的时候,跳过当前重复的元素。
Time Complexity - O(2n), Space Complexity - O(2n)。
- public class Solution {
- public List<List<Integer>> subsetsWithDup(int[] nums) {
- List<List<Integer>> res = new ArrayList<>();
- if(nums == null || nums.length == 0)
- return res;
- Arrays.sort(nums);
- ArrayList<Integer> list = new ArrayList<>();
- dfs(res, list, nums, 0);
- return res;
- }
- private void dfs(List<List<Integer>> res, ArrayList<Integer> list, int[] nums, int pos) {
- res.add(new ArrayList<Integer>(list));
- for(int i = pos; i < nums.length; i++) {
- if(i > pos && nums[i] == nums[i - 1])
- continue;
- list.add(nums[i]);
- dfs(res, list, nums, i + 1);
- list.remove(list.size() - 1);
- }
- }
- }
二刷:
跟Subsets I唯一不同的地方就是由重复。 和一刷一样,重点在于去重复。我们只需要在每一层遍历的时候,因为最开始sort过, 只要用i > pos && nums[i] == nums[i - 1]来跳过重复的元素就可以了。
Java:
Time Complexity - O(n!), Space Complexity (n2)
- public class Solution {
- public List<List<Integer>> subsetsWithDup(int[] nums) {
- List<List<Integer>> res = new ArrayList<>();
- if (nums == null || nums.length == 0) {
- return res;
- }
- Arrays.sort(nums);
- List<Integer> list = new ArrayList<>();
- subsetsWithDup(res, list, nums, 0);
- return res;
- }
- private void subsetsWithDup(List<List<Integer>> res, List<Integer> list, int[] nums, int pos) {
- res.add(new ArrayList<Integer>(list));
- for (int i = pos; i < nums.length; i++) {
- if (i > pos && nums[i] == nums[i - 1]) {
- continue;
- }
- list.add(nums[i]);
- subsetsWithDup(res, list, nums, i + 1);
- list.remove(list.size()- 1);
- }
- }
- }
三刷:
时间复杂度和空间复杂度真的不可以糊弄...看评论里shenhualong的解释比较好,基本的递归分析。 还要继续好好训练思维。 去重复的方法也可以用在其他类似题目里。
这里T(n) = T(n - 1) + T(n - 2) + ... + T(1), 因为 T(n - 1) = T(n - 2) + T(n - 3)... + T(1), 所以T(n) = 2 * T(n - 1),结果就是T(n) = 2n。 对于nums中的每一个数字,我们都要做一个Recursive call,所以这里用了O(n)的时间, 最终结果Time Complexity是 n * 2n。 空间复杂度的话因为我们每次要生成new ArrayList<>(),所以也是2n。
地里stellari大神的帖子分析得特别好,放在reference里了。
Java:
Time Complexity - O(n * 2n), Space Complexity (2n)
- public class Solution {
- public List<List<Integer>> subsetsWithDup(int[] nums) {
- List<List<Integer>> res = new ArrayList<>();
- if (nums == null) return res;
- Arrays.sort(nums);
- getSubsetsWithDup(res, new ArrayList<Integer>(), nums, 0);
- return res;
- }
- private void getSubsetsWithDup(List<List<Integer>> res, List<Integer> list, int[] nums, int pos) {
- res.add(new ArrayList<>(list));
- for (int i = pos; i < nums.length; i++) {
- if (i > pos && nums[i] == nums[i - 1]) continue;
- list.add(nums[i]);
- getSubsetsWithDup(res, list, nums, i + 1);
- list.remove(list.size() - 1);
- }
- }
- }
Reference:
http://blog.csdn.net/linhuanmars/article/details/24286377
https://leetcode.com/discuss/46668/recursive-iterative-manipulation-solutions-explanations
http://www.1point3acres.com/bbs/thread-117602-1-1.html
90. Subsets II的更多相关文章
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning
78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【LeetCode】90.Subsets II
Subsets II Given a collection of integers that might contain duplicates, nums, return all possible s ...
- LeetCode Problem 90. Subsets II
python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...
- 78. Subsets 90. Subsets II
1. Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...
- 90. Subsets II (Back-Track, DP)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- Leetcode#90 Subsets II
原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...
- LeetCode 90. Subsets II (子集合之二)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
随机推荐
- Python数据结构——栈、队列的实现(二)
1. 一个列表实现两个栈 class Twostacks(object): def __init__(self): self.stack=[] self.a_size=0 self.b_size=0 ...
- 1101. Quick Sort (25)
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- ASP.NET对HTML元素进行权限控制(三)
上一篇博客中有些没有考虑到的东西这次更改一下代码如下: 界面前台: <%@ Page Language="C#" AutoEventWireup="true&quo ...
- 再也不要说,jquery动画呆板了
1 show()方法和hide()方法 $("selector").show() 从display:none还原元素默认或已设置的display属性$("selecto ...
- android架构介绍
Android其本质就是在标准的Linux系统上增加了Java虚拟机Dalvik,并在Dalvik虚拟机上搭建了一个JAVA的application framework,所有的应用程序都是基于JAVA ...
- spring-mysqlclient开源了
https://github.com/risedragon/spring-mysqlclient/wiki/spring-mysqlclient-user-guide 开源了一个项目,总结了几年的数据 ...
- (转) linux目录结构详细介绍
转自:http://yangrong.blog.51cto.com/6945369/1288072 目录 1.树状目录结构图 2./目录 3./etc/目录 4./usr/目录 5./var/目录 6 ...
- WebAPi性能
提高WebAPi性能 前言 WebAPi作为接口请求的一种服务,当我们请求该服务时我们目标是需要快速获取该服务的数据响应,这种情况在大型项目中尤为常见,此时迫切需要提高WebAPi的响应机制,当然 ...
- Version of SQLite used in Android?
sing the emulators (adb shell sqlite3 --version): SQLite 3.7.11: 19-4.4-KitKat 18-4.3-Jelly Bean 17- ...
- Week2 Team Homework: 必应输入法的软件分析和用户需求调查
一.选题和目标人群的确定 4月8日,微软宣布推出首款整合搜索的中文云输入法“必应Bing输入法”,其前身是“英库拼音输入法”.微软宣称,该输入法界面干净,无广告.无插件,即使是在性能相对不高的电脑上, ...