这道题与没有重复项数字的全排列的解题思路一样,不过需要进行去重
#include <vector> class Solution { public: void recursion(set<vector<int>> &st, vector<int> &num, int index) { if(index == num.size()-1) { st.insert(num); } else { for(int i = index;i<num.size();++i) { //去重 if(i != index && num[i] == num[i-1]) continue; swap(num[i],num[index]); recursion(st, num, index+1); swap(num[i],num[index]); } } } vector<vector<int> > permuteUnique(vector<int> &num) { vector<vector<int> > res; sort(num.begin(),num.end()); set<vector<int>> st;//结果要以字典序升序排列,因此用set进行存储,如果不需要则直接用vector进行存储 recursion(st, num, 0); for(auto &r:st) res.push_back(r); return res; } };