Share my code with next_permutation


  • 0
    H

    very easy to understand.

    class Solution {
    public:
        vector<vector<int>> permuteUnique(vector<int>& nums) {
            vector<vector<int>> ans;
    	    sort(nums.begin(), nums.end());
    	    do{
    		    vector<int> tmp;
    		    for (int i = 0; i < nums.size(); ++i)
    		    {
    		        tmp.push_back(nums[i]);	
    		    }
    		    ans.push_back(tmp);
    	    }while(next_permutation(nums.begin(), nums.end()));
    	    return ans;
        }
    };

  • 0

    Why the tmp? You can just push nums.

        do{
            ans.push_back(nums);
        }while(next_permutation(nums.begin(), nums.end()));
    

Log in to reply
 

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