Given a list of numbers that may has duplicate numbers, return all possible subsets

Notice
  • Each element in a subset must be in non-descending order.
  • The ordering between two subsets is free.
  • The solution set must not contain duplicate subsets.
Example

If S = [1,2,2], a solution is:

[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
Challenge

Can you do it in both recursively and iteratively?

题意

给定一个可能具有重复数字的列表,返回其所有可能的子集

注意事项
  • 子集中的每个元素都是非降序的
  • 两个子集间的顺序是无关紧要的
  • 解集中不能包含重复子集

解法一:

 class Solution {
public:
/*
* @param nums: A set of numbers.
* @return: A list of lists. All valid subsets.
*/
vector<vector<int>> subsetsWithDup(vector<int> &nums) {
// write your code here
vector<vector<int> > results;
vector<int> result; sort(nums.begin(), nums.end()); helper(nums, , result, results); return results;
} void helper(vector<int> &nums, int start, vector<int> & result, vector<vector<int> > & results)
{
results.push_back(result); for (int i = start; i < nums.size(); ++i) {
if (i > start && nums[i] == nums[i - ]) {
continue;
} result.push_back(nums[i]); helper(nums, i + , result, results); result.pop_back();
}
}
};

18. Subsets II【medium】的更多相关文章

  1. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  2. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  3. 445. Add Two Numbers II【Medium】【两个链表求和】

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

  4. 221. Add Two Numbers II【medium】

    You have two numbers represented by a linked list, where each node contains a single digit. The digi ...

  5. 150. Best Time to Buy and Sell Stock II【medium】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. LeetCode:课程表II【210】

    LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...

  7. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  8. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

  9. LeetCode:全排列II【47】

    LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...

随机推荐

  1. Computer Vision Tutorials from Conferences (3) -- CVPR

    CVPR 2013 (http://www.pamitc.org/cvpr13/tutorials.php) Foundations of Spatial SpectroscopyJames Cogg ...

  2. 关于TagHelper的那些事情——自定义TagHelper(内嵌TagHelper)

    内嵌TagHelper 上一篇文章中提到有时候需要设计一种内嵌的TagHelper,如下: <my name="yy" age="35"> < ...

  3. SQL用例集锦

    SQL 语句分类 DDL - 数据定义语句 (CREATE,ALTER,DROP,DECLARE) DML - 数据操纵语句 (SELECT,DELETE,UPDATE,INSERT) DCL - 数 ...

  4. CentOS7下命令安装火狐浏览器

    使用命令安装火狐浏览器,需要切换root(su root)下,执行下面的命令,自动下载所需依赖包,完成安装 yum -y install firefox 然后重启即可

  5. [RSpec] LEVEL 2 CONFIGURATION & MATCHERS

    Installing RSpec In this level we'll start by getting you setup on a regular Ruby project, then move ...

  6. Vc++内存布局

    Vc++内存布局 测试平台 Windows server 2012 R2 and visual studio 2013 professional. 本篇文章意在介绍vc++中类的内存布局方式,只是研究 ...

  7. java线程同步问题——由腾讯笔试题引发的风波

    刚刚wm问我了一道线程的问题,因为自己一直是coder界里的渣渣.所以就须要恶补一下. 2016年4月2号题目例如以下. import java.util.logging.Handler; /** * ...

  8. C++ 11 - STL - 函数对象(Function Object) (中)

    我们再来看一个复杂的例子 需求: 我们需要对集合内每个元素加上一个特定的值 代码如下: AddInt.h class AddInt { private: int theValue; // the va ...

  9. QtGui.QSlider

    A QtGui.QSlider is a widget that has a simple handle. This handle can be pulled back and forth. This ...

  10. PHP开发学习门户第三版UI正式上线

    官网:http://www.phpthinking.com/ 论坛:http://bbs.phpthinking.com/ 迭代.迭代,似魔鬼的步伐.似魔鬼的步伐-- PHP开发学习门户第二版UI用了 ...