[leetcode]Permutations II

想用递归。。。结果感觉有点麻烦。。。

那要么就用stl的next_permutation吧,,,不过这样好没意思。。。

还是自己实现吧。。。

class Solution {
public:
    bool next_per(vector<int>& num) {
        int size = num.size();
        if(size <= 1) return false;
        int idx = size - 1;
        while(idx > 0 && num[idx] <= num[idx-1]) {
            idx --;
        }
        if(idx > 0) {
            idx --;
            int maxr = size - 1;
            while(num[maxr] <= num[idx]) maxr--;
            swap(num[idx] ,num[maxr] );
            reverse(num.begin() + idx + 1 , num.end());
        } else {
            return false;
        }
        return true;
    }
    vector<vector<int> > permuteUnique(vector<int> &num) {
        sort(num.begin() , num.end());
        vector<vector<int> >ans;
        ans.push_back(num);
        while(next_per(num)) {
            ans.push_back(num);
        }
        return ans;       
    }
};
原文地址:https://www.cnblogs.com/x1957/p/3514473.html