带重复元素的排列

给出一个具有重复数字的列表,找出列表所有不同的排列。

样例

给出列表 [1,2,2],不同的排列有:

[
  [1,2,2],
  [2,1,2],
  [2,2,1]
]
挑战

使用递归和非递归分别完成该题。


class Solution {
public:
    /*
     * @param :  A list of integers
     * @return: A list of unique permutations
     */
    vector> permuteUnique(vector &nums) {
        // write your code here
        vector> res;
        // vector visit(nums.size(), 0);
        // vector result;
        // sort nums
        sort(nums.begin(), nums.end());
        // permute(nums, 0, result, visit, res);
        // return res;
        // permute(nums, 0, res);
        // return res;
        // itself
        res.push_back(nums);
        while (nextPermute(nums)) {
            res.push_back(nums);
        }
        return res;
    }
    void permute(vector& nums, int level, vector& result,
                 vector &visit, vector> &res) {
        if (level == nums.size()) {
            res.push_back(result);
            return;
        }
        for (int i = 0; i < nums.size(); i++) {
            if (visit[i] > 0) {
                continue;
            }
            // skip duplicate
            // visit[i - 1] == 0 means the same value had been put at this level
            if (i > 0 && nums[i] == nums[i - 1] && visit[i - 1] == 0) {
                continue;
            }
            result.push_back(nums[i]);
            visit[i] = 1;
            permute(nums, level + 1, result, visit, res);
            visit[i] = 0;
            result.pop_back();
        }
    }
    bool needswap(vector &nums, int begin, int end, int target) {
        for (int i = begin; i <= end; i++) {
            if (nums[i] == target) {
                return false;
            }
        }
        return true;
    }
    void permute(vector& nums, int start, vector> &res) {
        if (start == nums.size()) {
            res.push_back(nums);
            return;
        }
        for (int i = start; i < nums.size(); i++) {
            // at this start, same value had been posted
            if (i > start && !needswap(nums, start, i - 1, nums[i])) {
                continue;
            }
            swap(nums[i], nums[start]);
            permute(nums, start + 1, res);
            swap(nums[i], nums[start]);
        }
    }
    void reverse(vector &nums, int begin, int end) {
        while (begin < end) {
            swap(nums[begin], nums[end]);
            begin++;
            end--;
        }
    }
    bool nextPermute(vector& nums) {
        int n = nums.size() - 1;
        int m = n - 1;
        // first increase order
        while (m >= 0 && nums[m] >= nums[m + 1]) {
            m--;
        }
        if (m >= 0) {
            // fist larger than nums[m]
            while (nums[n] <= nums[m]) {
                n--;
            }
            swap(nums[m], nums[n]);
        }
        // move to out of if because we need to recover nums
        reverse(nums, m + 1, nums.size() - 1);

        // return if we have next permute
        if (m >= 0) {
            return true;
        } else {
            return false;
        }
    }
};

你可能感兴趣的:(算法-排列组合,总结-深度优先)