46. 全排列
循环每次都从0开始
从0开始遍历used,来确定当前元素是否使用过
class Solution {
public:
void backtracking(vector<int>& nums,vector<bool> used)
{
if(path.size()==nums.size())
result.push_back(path);
for(int i=0;i<nums.size();i++)
{
//每次从头遍历used,重复的跳过
if(used[i]==true)continue;
used[i] = true;
path.push_back(nums[i]);
backtracking(nums,used);
path.pop_back();
used[i] = false;
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<bool> used(nums.size(),false);
backtracking(nums,used);
return result;
}
vector<int> path;
vector<vector<int>> result;