[leetcode] Permutations II
2013-08-10 18:29
351 查看
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
the following unique permutations:
and
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
set<vector<int> >ret;
int n=num.size();
_permute(num,n,n-1,ret);
return vector<vector<int> >(ret.begin(),ret.end());
}
void _permute(vector<int>& array,int n,int depth,vector<vector<int> >&ret){
if(depth==0){
ret.insert(array);
return;
}
for(int i=0 ; i<=depth ; i++){
swap(array[i],array[depth]);
_permute(array,n,depth-1,ret);
swap(array[i],array[depth]);
}
return;
}
};
For example,
[1,1,2]have
the following unique permutations:
[1,1,2],
[1,2,1],
and
[2,1,1].
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
set<vector<int> >ret;
int n=num.size();
_permute(num,n,n-1,ret);
return vector<vector<int> >(ret.begin(),ret.end());
}
void _permute(vector<int>& array,int n,int depth,vector<vector<int> >&ret){
if(depth==0){
ret.insert(array);
return;
}
for(int i=0 ; i<=depth ; i++){
swap(array[i],array[depth]);
_permute(array,n,depth-1,ret);
swap(array[i],array[depth]);
}
return;
}
};
相关文章推荐
- LeetCode 43.Multiply Strings & 46.Permutations & 47.Permutations II
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
- 【leetcode】 Permutations II
- leetcode-Permutations II
- LeetCode - Permutations II
- leetcode[47]Permutations II
- LeetCode(47)Permutations II
- LeetCode 47. Permutations II
- leetcode第一刷_Permutations II
- 【LeetCode】47. Permutations II
- Leetcode: Permutations II
- LeetCode47_Permutations II
- [LeetCode]题解(python):047-Permutations II
- leetcode - 47.Permutations II
- leetcode解题方案--047--Permutations II
- LeetCode: Permutations II
- leetcode 日经贴,Cpp code -Permutations II
- [LeetCode] Permutations II 全排列之二
- LeetCode - 47. Permutations II
- 【LeetCode】Permutations II