LeetCode 46

class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>>ans;
        vector<int>now;
        vector<bool>st(nums.size(),false);
        func(nums,ans,now,st,0,nums.size());
        return ans;
    }   
    void func(vector<int>& nums,vector<vector<int>>&ans,vector<int>&now,vector<bool>&st,int layor,int len)
    {
        if(layor==len)
        {
            ans.push_back(now);
            return;
        }
        for(int i=0;i<len;i++)
        {
            if(st[i]==true)
            {
                continue;
            }
            st[i]=true;
            now.push_back(nums[i]);
            func(nums,ans,now,st,layor+1,len);
            now.pop_back();
            st[i]=false;
        }
    } 
};

你可能感兴趣的:(LeetCode面试经典,leetcode,算法,数据结构)