My concise c++ solution without using recursion


  • 0
    S
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> result(1);
        for(int i = 0;i<nums.size();i++){
            generatenew(result,nums[i]);
        }
        return result;
    }
    
    void generatenew(vector<vector<int>> &result, int a){
        int size = result.size();
        int size_sub = result[0].size();
        for(int i = 0;i<size;i++){
            result[i].push_back(a);
            for(int j =0;j<size_sub;j++){
                vector<int> newform = result[i];
                swap(newform[j],newform[size_sub]);
                result.push_back(newform);
            }
        }
    }

Log in to reply
 

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