Leetcode Permutations II
2016-01-28 13:19
344 查看
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
and
Difficulty: Medium
回溯的基础上加一行判断。
For example,
[1,1,2]have the following unique permutations:
[1,1,2],
[1,2,1],
and
[2,1,1].
Difficulty: Medium
回溯的基础上加一行判断。
</pre><pre name="code" class="cpp">class Solution { public: void back_tracking(vector<vector<int> > &ans, vector<int> &temp, vector<int> &nums, vector<int> &flag){ if(temp.size() == nums.size()){ ans.push_back(temp); return; } for(int i = 0; i < flag.size(); i++){ if(flag[i] == 1 || (i > 0 && nums[i] == nums[i-1] && flag[i-1] == 0)) continue; temp.push_back(nums[i]); flag[i] = 1; back_tracking(ans, temp, nums, flag); flag[i] = 0; temp.pop_back(); } return; } vector<vector<int>> permuteUnique(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int> > ans; vector<int> temp; vector<int> flag(nums.size(), 0); back_tracking(ans, temp, nums, flag); return ans; } };
相关文章推荐
- EtherChannel(PAgP,LACP)
- deploy 自己编写的 QT 程序 (window下打包程序)
- 设计模式:适配器模式
- SQL Server管理及开发工具(Navicat for SQL Server)v11.2.13
- 1.3字符串-确定两串乱序同构
- NET 环境中使用RabbitMQ
- Linux 常见命令 --文件处理命令
- java中continue是什么意思?
- DrawWidget.cs
- Seek the Name, Seek the Fame(KMP算法之next数组的深入理解实例)
- 位运算 的探究
- 6.2 Controllers -- Representing Multipe Models
- sln里边的内容
- DrawMenus.cs
- LCA小结
- sl4j in project
- Use the endorsed directory mechanism to place jaxb-api.jar(jaxb-api.jar、jaxws-api.jar与jdk1.6不兼容问题)
- ElasticSearch 2 (3) - Breaking Changes
- TextEntity.cs
- 运用平行四边形法则解题二例