题目链接
https://leetcode.com/problems/permutations/
解题思路
dfs
代码
class Solution {
public:
void dfs(vector<vector<int>> &ans, vector<int> &nums, int ind) {
if (ind >= nums.size()) {
ans.push_back(nums);
}
for (int i = ind; i < nums.size(); ++i) {
swap(nums[ind], nums[i]);
dfs(ans, nums, ind + 1);
swap(nums[ind], nums[i]);
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> ans;
dfs(ans, nums, 0);
return ans;
}
};
网友评论