题目:

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],[]
]

链接:  http://leetcode.com/problems/subsets-ii/

题解:

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 1相关推荐

  1. 90 Subsets II

    90 Subsets II leetcode第90题,用深度优先搜索的思路写出了三种不同的代码.一道题目,同样是深度优先搜索的问题,因为看问题角度不同,思路不同,代码也不一样 /*** [1,2,2, ...

  2. LeetCode 90. Subsets II

    90. Subsets II Given a collection of integers that might contain duplicates, nums, return all possib ...

  3. 【暴力枚举】LeetCode 90. Subsets II

    LeetCode 90. Subsets II solution1和2均是利用set的,3和4是不利用set的 Solution1:我的答案 迭代法 class Solution { public:v ...

  4. 90. Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  5. [Leetcode]@python 90. Subsets II.py

    题目链接 https://leetcode.com/problems/subsets-ii/ 题目原文 Given a collection of integers that might contai ...

  6. 回溯模板+leetcode——78. 子集 + 90. 子集 II

    回溯法 一般情况下,看到题目要求「所有可能的结果」,而不是「结果的个数」,我们就知道需要暴力搜索所有的可行解了,可以用「回溯法」. 「回溯法」实际上一个类似枚举的搜索尝试过程,主要是在搜索尝试过程中寻 ...

  7. 递归/回溯:Subsets II求子集(有重复元素)

    上一篇描述了针对数组中没有重复元素进行子集的求取过程递归/回溯:subsets求子集 但是当出现如下数组时: 例如: nums[] = [2, 1, 2, 2] 结果为: [[], [1], [1,2 ...

  8. LeetCode 90.子集 II 中等难度

    90. 子集 II 题目: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例:输入: [1,2,2] 输出: [[2],[1],[ ...

  9. LeetCode 90. 子集 II【数组,回溯算法,排序去重】

    90. 子集 II 给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集). 解集 不能 包含重复的子集.返回的解集中,子集可以按 任意顺序 排列. 示例 1: 输入 ...

最新文章

  1. linux笔记本上安装了双显卡驱动(intel+nvidia)
  2. keras 模型简介
  3. 成功解决Exception “unhandled AttributeError“ module ‘h5py‘ has no attribute ‘File‘
  4. shields 徽标_徽标不够用时如何设计应用程序图标
  5. Android之PullToRefresh(ListView 、GridView 、WebView)使用详解和总结
  6. [TJOI2013]拯救小矮人(反悔贪心证明),「ICPC World Finals 2019」Hobson 的火车(基环树,差分)
  7. 几种常用的清除浮动方法(一)
  8. jQuery Ajax 如何设置Timeout
  9. NginxJava笔记-Webservice使用HTTPS协议及Spring Boot配置tomcat
  10. linux 上管理mysql_Linux下管理MySql
  11. Windows 下安装 MongoDB
  12. JAVA基础知识(五)数据类型转换
  13. 数据库注册两种方式的比较
  14. linux制作光盘镜像文件
  15. Python多线程编程方式1(转)
  16. Windows XP操作系统自带工具应用详解(转)
  17. SpringBoot实现阿里云短信接口对接
  18. 数仓建模—数据资产管理
  19. 2022-2028全球与中国工业蜂窝网关市场现状及未来发展趋势
  20. 买拍参考贴中羽主力拍统计

热门文章

  1. flutter - 如何在 dart/flutter 中收听流值
  2. 计算机二级vfp模拟考试题,计算机等级考试二级VFP模拟练习题[10]
  3. 如何进行有效的数据治理,提升数据价值?
  4. 神策数据荣获“年度最具影响力大数据服务厂商”奖项
  5. 桑文锋PMCAFF之行:数据驱动产品和运营决策
  6. 为不同目录设置Forms身份验证
  7. tinycore 的基本搭建,开机时间只需要1-3秒
  8. 在git中出现中文乱码的解决方案
  9. AC日记——[SDOI2010]大陆争霸 洛谷 P3690
  10. package.json