46. Permutations(回溯法)

class Solution {
public:
    void back(vector<vector<int>> &v,vector<int> & temp,vector<int> &nums)
    {
        if(nums.size()==0)
        {
            v.push_back(temp);
            return;
        }
        for(int i=0;i<nums.size();i++)
        {
            int tmp=nums[i];
            temp.push_back(nums[i]);
            nums.erase(nums.begin()+i);
            back(v,temp,nums);
            nums.insert(nums.begin()+i,tmp);
            temp.pop_back();
        }
    }
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> v;
        if(nums.size()==0) return v;
        //v.push_back(nums);
        else if (nums.size()==1) return {{nums[0]}};
        vector<int> temp;
        back(v,temp,nums);
        return v;
    }
};
    原文作者:回溯法
    原文地址: https://blog.csdn.net/u013480495/article/details/50937802
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞