Permutations II Solutions in C++
Number 47
Difficulty Medium
Acceptance 46.5%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/permutations-ii/// Author : Hao Chen// Date : 2014-06-21#include <stdio.h>#include <stdlib.h>#include <time.h>#include <iostream>#include <vector>#include <algorithm>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<vector<int> > permute(vector<int> &num) {vector<vector<int> > vv;vv.push_back(num);if (num.size() <2){return vv;}int pos=0;while(pos<num.size()-1){int size = vv.size();for(int i=0; i<size; i++){//sort the array, so that the same number will be togethersort(vv[i].begin()+pos, vv[i].end());//take each number to the firstfor (int j=pos+1; j<vv[i].size(); j++) {vector<int> v = vv[i];//skip the same numberif (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<int>& pt){cout << "{ ";for(int j=0; j<pt.size(); j++){cout << pt[j] << " ";}cout << "} " << endl;}int main(int argc, char** argv){int n = 3;if (argc>1){n = atoi(argv[1]);}srand(time(NULL));vector<int> v;for (int i=0; i<n; i++) {v.push_back(random()%n+1);}/*v[0] =0;v[1] =1;v[2] =0;v[3] =0;v[4] =9;*/printVector(v);cout << "-----------------" << endl;vector<vector<int> > vv;vv = permute(v);for(int i=0; i<vv.size(); i++) {printVector(vv[i]);}return 0;}
C++ solution by pezy/LeetCode
#include <vector>using std::vector;#include <algorithm>using std::next_permutation; using std::sort;class Solution {public:vector<vector<int> > permuteUnique(vector<int> &num) {vector<vector<int>> retv;sort(num.begin(), num.end());do {retv.push_back(num);} while (next_permutation(num.begin(), num.end()));return retv;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/permutations-ii/description//// Author : liuyubobobo/// Time : 2017-12-18#include <iostream>#include <vector>using namespace std;/// Most Naive Recursive/// Time Complexity: O(n^n)/// Space Complexity: O(n)class Solution {private:vector<vector<int>> res;vector<bool> used;public:vector<vector<int>> permuteUnique(vector<int>& nums) {res.clear();if(nums.size() == 0)return res;used = vector<bool>(nums.size(), false);sort(nums.begin(), nums.end());vector<int> p;generatePermutation(nums, 0, p);return res;}private:void generatePermutation(const vector<int>& nums, int index, vector<int> &p){if(index == nums.size()){res.push_back(p);return;}for(int i = 0 ; i < nums.size() ; i ++)if(!used[i]){if(i > 0 && nums[i] == nums[i-1] && !used[i-1])continue;p.push_back(nums[i]);used[i] = true;generatePermutation(nums, index + 1, p);p.pop_back();used[i] = false;}}};void printVec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {vector<int> nums1 = {1, 1, 2};vector<vector<int>> res1 = Solution().permuteUnique(nums1);for(const vector<int>& tres: res1)printVec(tres);vector<int> nums2 = {2, 2, 1, 1};vector<vector<int>> res2 = Solution().permuteUnique(nums2);for(const vector<int>& tres: res2)printVec(tres);return 0;}