47. Permutations II
来源:互联网 发布:免费chinanet软件2017 编辑:程序博客网 时间:2024/06/08 19:06
题目
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2] have the following unique permutations: [
[1,1,2],
[1,2,1],
[2,1,1]
]
解析
题目:对一个数组进行全排列,数组有重复的数字
思路:使用回溯法,用map根据数组的下标进行回溯,在判断结果是去重即可
代码
class Solution {public: vector<vector<int>> permuteUnique(vector<int>& nums) { vector<vector<int>> ret; vector<int> tmp; unordered_map<int,bool> m; m[0]=m[1]=m[2]=false; //数组下标 help(ret,tmp,m,nums); return ret; } void help(vector<vector<int>> &ret,vector<int> &tmp,unordered_map<int,bool> &m,vector<int> &nums){ if(tmp.size()==nums.size()&&find(ret.begin(),ret.end(),tmp)==ret.end()){ //去重 ret.push_back(tmp); return; } for(int i=0;i<nums.size();i++){ if(!m[i]){ tmp.push_back(nums[i]); m[i]=true; help(ret,tmp,m,nums); tmp.pop_back(); m[i]=false; } } }};
阅读全文
0 0
- 46. Permutations && 47. Permutations II
- 46. Permutations, 47. Permutations II
- [LeetCode]47.Permutations II
- LeetCode 47.Permutations II
- LeetCode --- 47. Permutations II
- [Leetcode] 47. Permutations II
- [leetcode] 47.Permutations II
- 47.Permutations II
- 47. Permutations II
- LeetCode 47. Permutations II
- 47. Permutations II
- 47. Permutations II LeetCode
- 47. Permutations II
- 47. Permutations II
- 47. Permutations II
- 47. Permutations II
- Leetcode 47. Permutations II
- 47. Permutations II
- LightOJ 1220
- 微信开发<1>——图像拍照接口实现
- 简洁明了的Docker入门(转载)
- LeetCode167. Two Sum II
- requirejs小结
- 47. Permutations II
- java初学写一个快排压压惊
- 什么是阻塞式和非阻塞io流?
- php拼音检索
- linux命令整理
- java 模板 单例
- 在 VMware WorkStation Pro 12 中创建虚拟机
- Python实现The Most Wanted Letter详解
- Connections Gym