LeetCode46. 全排列

题意

求一个序列的全排列

方法

DFS

代码

class Solution {
public:
    vector<vector<int>> ans;
    vector<bool> vis{0, 0, 0, 0, 0, 0, 0};

    void permutation(vector<int> per, vector<int> nums)
    {
        int len = nums.size();
        if (per.size() == len) {
            ans.push_back(per);
            return;
        }

        for (int i = 0; i < len; i++) {
            if (!vis[i]) {
                vis[i] = true;
                per.push_back(nums[i]);
                permutation(per, nums);
                per.pop_back();
                vis[i] = false;
            }
        }
    }

    vector<vector<int>> permute(vector<int>& nums) {
        vector<int> per;
        permutation(per, nums);

        return ans;
    }
};

你可能感兴趣的:(hot,100,深度优先,leetcode,算法)