用next_permutation的方法
class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
vector<vector<int> > res;
if(num.size() <= 1){
res.push_back(num);
return res;
}
sort( num.begin(), num.end());//一定要对数组排序
res.push_back(num);
while(true){//还要加个循环,不然i和ii走一遍就结束了
int i = num.size()-1;
while(true){
int ii = i;
--i;
if( num[i] < num[ii]){
int j = num.size();
while( !(num[i] < num[--j])){}
swap(num, i, j);
reverse( num, ii);
res.push_back(num);
break;
}
if( i == 0){
return res;
}
}
}
}
void swap( vector<int> &num, int i, int j){
int tmp = num[i];
num[i] = num[j];
num[j] = tmp;
}
void reverse( vector<int> &num, int i){
for( int j = i, k = num.size()-1; j < k; ++j, --k)
swap( num, j, k);
}
};