My C++ Solution With STL and less than 18 lines


  • -2
    L
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        vector<vector<int> >res;
        vector<int> per(nums.size(),0);
        for(int i=1,con=0;i<nums.size();i++){
            if(nums[i]!=nums[i-1]) per[i]=con=i;
            else                   per[i]=con;
        }
        do{
            vector<int> tmp(nums.size());
            for(int i=0;i<nums.size();i++)
                tmp[i]=nums[per[i]];
            res.push_back(tmp);
        }while(next_permutation(per.begin(),per.end() ));
        return res;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.