leetcode笔记:Subsets II
2015-12-29 23:48
260 查看
一. 题目描述
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
二. 题目分析
该题相比Subsets一题略有不同,该题在给定的数组中,允许出现重复的元素。但最后输出的排列不能有重复的组合。因此,在DFS时,使用一个数组temp来记录某个数是否被使用过即可。
三. 示例代码
四. 小结
该题需要注意的地方是,避免出现同样的排列组合。
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:
[code][ [2], [1], [1,2,2], [2,2], [1,2], [] ]
二. 题目分析
该题相比Subsets一题略有不同,该题在给定的数组中,允许出现重复的元素。但最后输出的排列不能有重复的组合。因此,在DFS时,使用一个数组temp来记录某个数是否被使用过即可。
三. 示例代码
[code]#include <iostream> #include <vector> using namespace std; class Solution { public: vector<vector<int> > subsetsWithDup(vector<int> nums) { sort(nums.begin(), nums.end()); subsets(nums, 0); return result; } private: vector<int> temp; vector<vector<int> > result; void subsets(vector<int> nums, int index){ if(index == nums.size()) { result.push_back(temp); return; } if(temp.size() == 0 || temp[temp.size()-1] != nums[index]) subsets(nums, index + 1); temp.push_back(nums[index]); subsets(nums, index + 1); temp.pop_back(); } };
四. 小结
该题需要注意的地方是,避免出现同样的排列组合。
相关文章推荐
- php与Microsoft SQL Server
- 技术安家了
- win10下启动mysql解压版
- ViewPager使用详解
- 多声道音频指南(二):那些年,那些技术,那些名词
- 关于Xcode7中添加不了libresolv.dylib等类似库的问题
- JVM启动参数大全
- empty()函数经典详解
- 数据结构 JAVA描述(四) 树与二叉树基础
- 计数器
- windows下批量执行sql文件
- win8X64下自行搭建PHP开发环境
- 2015年小结
- activiti的一些记录
- 安装、配置、使用kermit
- Oracle远程连接设置
- Linux(Centos)Hi3516交叉编译问题
- 多声道音频指南(一):被声音包围的感觉
- C++接口类实现的简单例子(1)
- 屏蔽setChecked方法对OnCheckedChangeListener的影响(CheckBox,SwitchButton)