Easy C++ solution


  • 0
    C

    Since order of permutation doesn't matter here,

        vector<vector<int> > a;
        void solve(int i,vector<int>&p, vector<int>&nums)
        {
            if(i==nums.size())
            {
                a.push_back(p);
                return;
            }
            
            p.push_back(nums[i]);
            solve(i+1,p,nums);
            p.pop_back();
            
            vector<int>::iterator it=p.begin();
            
            for(int j=0;j<p.size();++j)
            {
                p.insert(it+j,nums[i]);
                solve(i+1,p,nums);
                p.erase(it+j);
            }
        }
        
        vector<vector<int>> permute(vector<int>& nums) {
            a.clear();
            vector<int> p;
            solve(0,p,nums);
           
            return a;
        }
    };
    

Log in to reply
 

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