// Source : https://oj.leetcode.com/problems/permutations-ii/ // Author : Hao Chen // Date : 2014-06-21 /********************************************************************************** * * 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]. * * **********************************************************************************/ #include #include #include #include #include #include using namespace std; // To deal with the duplication number, we need do those modifications: // 1) sort the array [pos..n]. // 2) skip the same number. vector > permute(vector &num) { vector > vv; vv.push_back(num); if (num.size() <2){ return vv; } int pos=0; while(pos v = vv[i]; //skip the same number if (j>0 && v[j]==v[j-1]){ continue; } int t = v[j]; v[j] = v[pos]; v[pos] = t; vv.push_back(v); } } pos++; } return vv; } void printVector( vector& pt) { cout << "{ "; for(int j=0; j1){ n = atoi(argv[1]); } srand(time(NULL)); vector v; for (int i=0; i > vv; vv = permute(v); for(int i=0; i