Permutations II
2015-07-25 13:45
330 查看
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
Solution:
For example,
[1,1,2]have the following unique permutations:
[1,1,2],
[1,2,1], and
[2,1,1].
Solution:
class Solution { public: void dfs(vector<vector<int>> &res, vector<int> &p, vector<int> &nums) { if(nums.size() == 0) { res.push_back(p); return ; } for(int i = 0; i < nums.size(); ) { int index = nums[i]; int next = i + 1; while(next < nums.size() && nums[next] == index) next++; p.push_back(index); nums.erase(nums.begin() + i); dfs(res, p, nums); nums.insert(nums.begin() + i, index); p.pop_back(); i = next; } } vector<vector<int>> permuteUnique(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int>> res; vector<int> p; dfs(res, p, nums); return res; } };
相关文章推荐
- 我来自阿里政委,一个神秘的组织
- 图结构练习——判断给定图是否存在合法拓扑序列
- VLC框架分析
- Mooc认证
- HDU 1864
- OpenSAP网址收藏
- ASP.NET中Session的sessionState 4种mode模式
- vsftpd安装与配置--研究tcp与防火墙
- linux下解压war格式的包
- Android之Monkey全参数
- python RESTful API框架:Eve 快速入门
- hdoj 1285 确定比赛名次(拓扑排序)
- Linux学习二
- OSG粒子系统应用:喷泉效果
- UVa 11584 - Partitioning by Palindromes(DP)
- swift语言中一些常用的运算符
- OC学习笔记——类别(Category)
- HTML之框架(frameest、ifram)
- hdu1213 How Many Tables(并查集)
- SpringMVC简单项目配置