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.
Example:
Input: [1,2,2]
Output:
[[2],[1],[1,2,2],[2,2],[1,2],[]
]
难度:medium
题目:给定一可能含有重复元素的集合,返回所有可能的子集合。
注意:答案不能含有重复子集。
思路:递归,排序去重
Runtime: 3 ms, faster than 45.31% of Java online submissions for Subsets II.
Memory Usage: 35.7 MB, less than 0.97% of Java online submissions for Subsets II.
class Solution {public List<List<Integer>> subsetsWithDup(int[] nums) {List<List<Integer>> result = new ArrayList<>();Arrays.sort(nums);for (int i = 0; i < nums.length; i++) {subsetsWithDup(nums, 0, i + 1, new Stack<Integer>(), result);}result.add(new ArrayList<>());return result;}public void subsetsWithDup(int[] nums, int idx, int count, Stack<Integer> stack, List<List<Integer>> result) {if (count <= 0) {result.add(new ArrayList<>(stack));return;}for (int i = idx; i < nums.length - count + 1; i++) {if (i == idx || nums[i - 1] != nums[i]) {stack.push(nums[i]);subsetsWithDup(nums, i + 1, count - 1, stack, result);stack.pop();}}}
}
90. Subsets II相关推荐
- 90 Subsets II
90 Subsets II leetcode第90题,用深度优先搜索的思路写出了三种不同的代码.一道题目,同样是深度优先搜索的问题,因为看问题角度不同,思路不同,代码也不一样 /*** [1,2,2, ...
- LeetCode 90. Subsets II
90. Subsets II Given a collection of integers that might contain duplicates, nums, return all possib ...
- 【暴力枚举】LeetCode 90. Subsets II
LeetCode 90. Subsets II solution1和2均是利用set的,3和4是不利用set的 Solution1:我的答案 迭代法 class Solution { public:v ...
- 90. Subsets II 1
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- [Leetcode]@python 90. Subsets II.py
题目链接 https://leetcode.com/problems/subsets-ii/ 题目原文 Given a collection of integers that might contai ...
- 回溯模板+leetcode——78. 子集 + 90. 子集 II
回溯法 一般情况下,看到题目要求「所有可能的结果」,而不是「结果的个数」,我们就知道需要暴力搜索所有的可行解了,可以用「回溯法」. 「回溯法」实际上一个类似枚举的搜索尝试过程,主要是在搜索尝试过程中寻 ...
- 递归/回溯:Subsets II求子集(有重复元素)
上一篇描述了针对数组中没有重复元素进行子集的求取过程递归/回溯:subsets求子集 但是当出现如下数组时: 例如: nums[] = [2, 1, 2, 2] 结果为: [[], [1], [1,2 ...
- LeetCode 90.子集 II 中等难度
90. 子集 II 题目: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例:输入: [1,2,2] 输出: [[2],[1],[ ...
- LeetCode 90. 子集 II【数组,回溯算法,排序去重】
90. 子集 II 给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集). 解集 不能 包含重复的子集.返回的解集中,子集可以按 任意顺序 排列. 示例 1: 输入 ...
最新文章
- x9此计算机上没有hasp_mastercam x9安装步骤
- Hr必看:也许你做的绩效考核都是错的
- webview添加网页加载进度条
- 10个月产品演化之路-快速试错,快速反应,探索产品成功之道
- C++ cin 实现循环读入
- 注册COM组件cmd(管理员权限)
- lightning接口_Lightning太赚钱?iPhone永远不会用Type-C
- Linux修改hostname的几种方式,及遇到的问题
- 关于标准输入输出流和string类
- 怎样写一个具有异步交互的React组件的单元测试
- 穿了好几个马甲,差点没认出来是二分查找
- Bug管理的流程和几个重点
- AtCoder Grand Contest 017D (AGC017D) Game on Tree 博弈
- postman 的基础使用
- 汇编程序16位带符号变量计算
- iphone 如何卸载xcode4.2
- idea断点的几个意思_你真的理解Intellij IDEA的Debug技巧吗?这篇文章全部教给你...
- linux系统数据库导出语句,数据库应用-SQL语句导入导出大全
- c 语言 字符串 替换,将字符串中的字符替换成指定字符c
- 工程流体力学笔记暂记17(漩涡的基本概念+求解漩涡强度的斯托克斯定理)