LeetCode OJ:Permutations II

1261 단어 LeetCode
Permutations II  
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example, [1,1,2]  have the following unique permutations: [1,1,2][1,2,1] , and  [2,1,1] .
next 사용permutation 함수
같다
 
LeetCode OJ:Permutations
class Solution {
    vector<vector<int>> result;
    vector<bool> visit;
    vector<int> res;
public:
    void dfs(int k,int n,vector<int> &num){  
        if(k>n){  
            result.push_back(res);  
            return;  
        }  
        for(int i=0;i<=n;i++){  
            if(!visit[i]){
                if (i != 0 && num[i] == num[i-1] && !visit[i-1])
                    continue;
                visit[i]=true;
                res.push_back(num[i]);
                dfs(k+1,n,num);  
                res.pop_back();
                visit[i]=false;
            }
        }  
    }  
    vector<vector<int> > permuteUnique(vector<int> &num) {
        visit.assign(num.size(),false);
        sort(num.begin(),num.end());
        dfs(0,num.size()-1,num);
        return result;
    }
};

좋은 웹페이지 즐겨찾기