leetcode 46. Permutations DFS

题目

https://leetcode.com/problems/permutations/

思路

dfs遍历 去重看上一个

源码

class Solution {
public:
    vector<vector<int>> res;
    vector<vector<int>> permute(vector<int>& nums) {
        vector<bool> vis(nums.size(),false);
        vector<int>path;
        dfs(nums,path,vis);
        return res;
    }
    void dfs(vector<int>& nums,vector<int>& path,vector<bool> vis){
        if(path.size()==nums.size()){
            res.push_back(path);
            return;
        }
        for(int i = 0;i < nums.size();++i){
            if(vis[i])continue;
            path.push_back(nums[i]);
            vis[i]=true;
            dfs(nums,path,vis);
            vis[i]=false;
            path.pop_back();
        }
    }
};

发表评论

邮箱地址不会被公开。 必填项已用*标注