## 题目

``````输入：nums = [1,2,3]

``````

``````输入：nums = [0,1]

``````

``````输入：nums = [1]

``````

`````` 1 <= nums.length <= 6
2. -10 <= nums[i] <= 10
3. nums 中的所有整数 互不相同
``````

## 代码

``````class Solution {

public:
vector<vector<int>> ans;
vector<int> temp;
int n;
vector<vector<int>> permute(vector<int>& nums) {

n = nums.size();
vector<bool> record(n, false);
dfs(0, nums, record);
return ans;
}
};
``````

``````void dfs(int u, vector<int>& nums, vector<bool>& record) {

if(u == n) {

ans.push_back(temp);
return;
}
}
``````

``````void dfs(int u, vector<int>& nums, vector<bool>& record) {

if(u == n) {

ans.push_back(temp);
return;
}
for(int i = 0; i < n; i++) {

if(!record[i]) {

record[i] = true;
temp.push_back(nums[i]);
dfs(u + 1, nums, record);
temp.pop_back();
record[i] = false;
}
}
}
``````

``````class Solution {

public:
vector<vector<int>> ans;
vector<int> temp;
int n;
void dfs(int u, vector<int>& nums, vector<bool>& record) {

if(u == n) {

ans.push_back(temp);
return;
}
for(int i = 0; i < n; i++) {

if(!record[i]) {

record[i] = true;
temp.push_back(nums[i]);
dfs(u + 1, nums, record);
temp.pop_back();
record[i] = false;
}

}
}
vector<vector<int>> permute(vector<int>& nums) {

n = nums.size();
vector<bool> record(n, false);
dfs(0, nums, record);
return ans;
}
};
``````

### Guess you like

Origin blog.csdn.net/m0_61607810/article/details/121218308
Recommended
Ranking
Daily