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;
}
};
【leetcode 回溯dfs C++】46. Permutations
猜你喜欢
转载自blog.csdn.net/m0_37454852/article/details/114309564
今日推荐
周排行