占坑. Write it later.
It's nearly impossible to model algorithms by mathematics. We can only pursue progressive rigor(actually, maybe we cannot call it mathematics).
https://leetcode.com/problems/permutations/description/
Backtrace
class Solution {
public:
vector<vector<int>> res;
vector<int> permutation;
void backtrace(bool decision[], vector<int>& nums)
{
if (permutation.size() == nums.size())
{
res.push_back(permutation);
}
else
{
for (int i = 0; i < nums.size(); ++i)
{
if (decision[i]) continue;
permutation.push_back(nums[i]);
decision[i] = true;
backtrace(decision, nums);
decision[i] = false;
permutation.pop_back();
}
}
}
vector<vector<int>> permute(vector<int>& nums) {
bool decision[nums.size()+1];
backtrace(decision, nums);
return res;
}
};
标签:nums,Backtrace,decision,vector,permutation,backtrace,size
From: https://www.cnblogs.com/qianxinn/p/17641732.html