Permutations II

Posted summerkiki

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Permutations II相关的知识,希望对你有一定的参考价值。

class Solution {
public:
    void swap(int& a, int&b)
    {
        int temp=a;
        a=b;
        b=temp;
    }
    bool ifduplicate(vector<int> nums, int n, int i)
    {
        while(n<i)
        {
            if(nums[n]==nums[i])
                return false;
            n++;
        }
        return true;
    }
    void permutation(vector<vector<int>>& res, vector<int>& nums, int n)
    {
        if(n==nums.size()-1)
        {
            res.push_back(nums);
            return;
        }
        
        for(int i=n;i<nums.size();i++)
        {
            if(!ifduplicate(nums,n,i))
                continue;
            swap(nums[i],nums[n]);
            permutation(res, nums, n+1);
            swap(nums[i],nums[n]);
        }
        
    }
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        vector<vector<int>> res;
        if(nums.size()==0)
            return res;
        permutation(res,nums,0);
        return res;
    }
};

 

以上是关于Permutations II的主要内容,如果未能解决你的问题,请参考以下文章

leetcode-Permutations II-47

[Lintcode]16. Permutations II/[Leetcode]47. Permutations II

LeetCode46,47 Permutations, Permutations II

leetcode 46-Permutations and 47-Permutations II

[Leetcode] Permutations II

LintCode : Permutations II