18. Subsets II【medium】
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.
If S = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
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】的更多相关文章
- 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 ...
- 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 ...
- 445. Add Two Numbers II【Medium】【两个链表求和】
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- 221. Add Two Numbers II【medium】
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- 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 ...
- LeetCode:课程表II【210】
LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- LeetCode:全排列II【47】
LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...
随机推荐
- 如何写科技论文How to write a technical paper
This is the evolving set of recommendations I share with my graduate students for technical writing. ...
- C++对C的改进(1)
本文地址:http://www.cnblogs.com/archimedes/p/cpp-change1.html,转载请注明源地址 新的初始化方法 C提供的初始化方法 int x = 1024; C ...
- Node Server零基础——开发环境文件自动重载
收录待用,修改转载已取得腾讯云授权 前言 在 web 前端开发中,我们会借助 Grunt.Gulp 和 Webpack 等工具的 Watch 模块去监听文件变化,那服务端应该怎么做?其实文件变化的监听 ...
- Opencv 完美配置攻略 2014 (Win8.1 + Opencv 2.4.8 + VS 2013)
http://my.phirobot.com/blog/2014-02-opencv_configuration_in_vs.html 2012年4月给同学写了篇傻瓜式的 VS2010+Opencv- ...
- html5图表
http://www.html5tricks.com/tag/html5图表
- Objective-C中3种枚举比较及KVC两个小技巧
Objective-C中3种枚举比较及KVO两个小技巧 一:oc的3种枚举 for循环 for in 枚举块 如代码 NSUInteger totalCount = 10000; NSMutableA ...
- 算法笔记_012:埃拉托色尼筛选法(Java)
1 问题描述 Compute the Greatest Common Divisor of Two Integers using Sieve of Eratosthenes. 翻译:使用埃拉托色尼筛选 ...
- VB 获取默认打印机的状态
如何获取默认打印机的状态,包括缺纸.卡纸.无连接等状态,还有将某文件打印后,如何得知打印成功? Option ExplicitDeclare Function MapPhysToLin Lib &qu ...
- JSON格式校验
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- java接口的高级应用
直接上菜 /*接口类*/ public interface MsgListener{ public void afterMsgRecived(String msgData); } /*工具类*/ pu ...