递归与回溯:[46. 全排列](https://leetcode-cn.com/problems/permutations/)
```c++
//C++版本
#include<iostream>
#include<vector>
using namespace std;
// pair<int, int> t(0, 0);
vector<int> path;
vector<int> path_note(9, 0);
vector<vector<int> > res;
void traversal(int k, int startIndex);
int main(){
int n = 0;
cin >> n;
traversal(n, 1);
for(int i = 0; i < res.size(); i++){
for(int j = 0; j < res[i].size(); j++)
cout << res[i][j] << ' ';
cout << '\n';
}
return 0;
}
void traversal(int k, int startIndex){
if(path.size() == k){
res.push_back(path);
return;
}
for(int i = startIndex; i <= k; i++){
if(path_note[i] == 1)
continue;
path.push_back(i);
path_note[i] = 1;
traversal(k, 1);
path.pop_back();
path_note[i] = 0;
}
}
```
```java
//JAVA版本
class Solution {
public List<Integer> path = new ArrayList<>();
boolean[] used;
public List<List<Integer> > res = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
path.clear();
res.clear();
used = new boolean[nums.length];//pay attention
traversal(nums);
return res;
}
public void traversal(int[] nums){
if(path.size() == nums.length){
res.add(new LinkedList<>(path));
return;
}
for(int i = 0; i < nums.length; i++){
if(used[i] == true)
continue;
path.add(nums[i]);
used[i] = true;
traversal(nums);
path.remove(path.size() - 1);
used[i] = false;
}
}
}
```