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 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...
随机推荐
- iOS: xcode打包上传iTunes失败,iTunes Store operation failed,this action can not complete .try again
通过xcode点击“upload to app store”上传到itunes,结果一直提示“itunes store operation failed” 原因:网速的问题,我之前也遇到过,网速好的时 ...
- iOS:分组的表格视图UITableView,可以折叠和展开
虽然表格视图可以分组,但是如果分组后,每一行的内容太多,往后翻看起来比较的麻烦.为了解决这个麻烦,可以将分组的行折叠和展开.折叠时,行内容就会隐藏起来:展开时,行内容就会显示出来. 折叠时: 展开后: ...
- 给Linux初学者的七个建议,值得一读
刚开始使用Linux时你也许会感到不习惯,许多高手也都有这样的经历.毕竟,曾经他们也都是新手.但是现在Linux团体已经在为新用户提供有关指南, 手册和基本的技术文档来使他们尽快上手方面做的相当出色. ...
- [Angular-Scaled web] 5. ui-router $stateParams for sharing information
When using ui-route, we want to pass the information with the url. Example: angular.module('categori ...
- Servlet对文件的读写操作
(1)怎样在serlvet中读取文件的内容 package com.tsinghua; import java.io.*; import javax.servlet.http.*; public cl ...
- 求证:a^4+b^4 ≧a^3*b+a*b^3
证明: a4+b4-a3b-ab3 =a3(a-b)-b3(a-b) =(a3-b3)(a-b) =(a-b)2(a2+ab+b2) 而a2+ab+b2=a2+ab+b2/4+3b2/4=(a+b/2 ...
- tasklist、taskkill、taskmgr
1.tasklist 列出所有的进程,使用tasklist |findstr xxx , 选取进程 2.taskkill 杀掉进程,使用 taskkill /f /pid 1235 3.taskmg ...
- JUnit 3.8 让所有测试程序 实现 复合的测试(TestSuite)
之前是单个单个程序测试,这种方式在测试类比较少的时候可行, 但测试类多了,单个单个的这个测试方式就不推荐了,那得使用 复合的测试了 一个TestSuite是一个复合的测试.它运行测试用例集. 这个 ...
- chrome 此网页正试图从未经验证的来源加载脚本
chrome 此网页正试图从未经验证的来源加载脚本 CreateTime--2018年5月25日08点02分 Author:Marydon 1.情景还原 以我的网站为例,https://www.c ...
- CRNN中英文字符识别
代码地址如下:http://www.demodashi.com/demo/13870.html 参考GitHub源码:https://github.com/YoungMiao/crnn 应demo大师 ...