首页 > 代码库 > Leetcode--permutations II
Leetcode--permutations II
Problem Description:
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]
, and [2,1,1]
.
class Solution { public: void swap(int &a,int &b) { int temp=a; a=b; b=temp; } bool Isduplicate(vector<int> num,int begin,int last) { for(vector<int>::size_type i=begin;i!=last;++i) if(num[i]==num[last]) return false; return true; } void permutations(vector<int> num,int begin,vector<int> &permutation,vector<vector<int> > &res) { if(begin==num.size()) { res.push_back(permutation); return; } for(vector<int>::size_type index=begin;index!=num.size();++index) { if(Isduplicate(num,begin,index)) { swap(num[begin],num[index]); permutation.push_back(num[begin]); permutations(num,begin+1,permutation,res); permutation.pop_back(); swap(num[begin],num[index]); } } } vector<vector<int> > permuteUnique(vector<int> &num) { vector<vector<int> > res; if(num.empty()) return res; vector<int> permutation; permutations(num,0,permutation,res); return res; } };
Leetcode--permutations II
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。