generate unique permutations (Interview problem)

Revision en1, by acash, 2019-11-01 14:52:01

Given a collection of numbers that might contain duplicates, return all possible unique permutations. Input: [1,1,2] Output: [ [1,1,2], [1,2,1], [2,1,1] ] I am failing on a large test cases can anyone help me where i am doing wrong? please:) ~~~~~ class Solution { public: void solve(vector<vector>&res,int j,vector&nums){ if(j==nums.size()){ res.push_back(nums); return; } for(int i=j;i<nums.size();i++){ if(i==j||nums[i]!=nums[j]){ swap(nums[i],nums[j]); solve(res,j+1,nums); swap(nums[i],nums[j]);} } } vector<vector> permuteUnique(vector& nums) { vector<vector>res; sort(nums.begin(),nums.end()); solve(res,0,nums); return res; } }; ~~~~~

Tags backtracking, cpp11

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English acash 2019-11-01 14:54:43 4 Tiny change: 'please:)\n~~~~~\nc' -> 'please:)\n\n\n~~~~~\nc'
en1 English acash 2019-11-01 14:52:01 908 Initial revision (published)