leetcode-----46. 全排列

  • 链接:https://leetcode-cn.com/problems/permutations/

代码

class Solution {
public:
    vector> ans;
    vector path;
    vector st;

    vector> permute(vector& nums) {
        int n = nums.size();
        path = vector(n);
        st = vector(n);
        
        dfs(nums, 0);
        return ans;    
    }

    void dfs(vector& nums, int u) {
        if (u == nums.size()) {
            ans.push_back(path);
            return;
        }

        for (int i = 0; i < nums.size(); ++i) {
            if (st[i] == false) {
                path[u] = nums[i];
                st[i] = true;
                dfs(nums, u + 1);
                st[i] = false;
            }
        }
    }
};

你可能感兴趣的:(leetcode-----46. 全排列)