C++ solution, just implement the meaning of permutation, No swap;


  • 2
    1
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> result;
        const int size=nums.size();
        bool used[size]={false};
        vector<int> tmp;
        generate(result,used,nums,tmp);
        return result;
    }
    void generate(vector<vector<int>> &result,bool used[],vector<int> &nums,vector<int> &tmp){
        int count=0;
        for(int i=0;i<nums.size();++i){
            if(!used[i]){
                used[i]=true;
                tmp.push_back(nums[i]);
                generate(result,used,nums,tmp);
                used[i]=false;
                tmp.pop_back();
                ++count;
            }
        }
        if(count==0)
            result.push_back(tmp);
    }

Log in to reply
 

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