【leetcode 回溯dfs C++】46. Permutations

46. Permutations

【leetcode 回溯dfs C++】46. Permutations

class Solution {
public:
    vector<vector<int>> ans;
    void dfs(vector<int>& nums, vector<bool>& visited, vector<int>& V) {
        bool flag = true;
        for(int ii = 0; ii < visited.size(); ii++) {
            if(!visited[ii]) {
                V.push_back(nums[ii]);
                visited[ii] = true;
                dfs(nums, visited, V);
                V.pop_back();
                visited[ii] = false;
                flag = false;
            }
        }
        if(flag) ans.push_back(V);
    }
    vector<vector<int>> permute(vector<int>& nums) {
        vector<bool> visited(nums.size(), false);
        vector<int> V;
        dfs(nums, visited, V);
        return ans;
    }
};
上一篇:快点来白嫖!46道面试题带你了解中高级Android面试,架构师必备技能


下一篇:堆 46