Iterative C++11 solution


  • 5
    R

    The move semantic is sometimes helpful.

    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> v(1,vector<int>());
        for (int i = 0; i < nums.size(); ++i) {
            vector<vector<int>> tv(move(v));
            for (int j = 0; j < tv.size(); ++j) {
                for (int k = 0; k <= tv[0].size(); ++k) {
                    vector<int> tmp(tv[j]);
                    tmp.insert(tmp.begin()+k,nums[i]);
                    v.push_back(move(tmp));
                }
            }
        }
        return v;
    }

Log in to reply
 

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