![在这里插入图片描述](https://img-blog.csdnimg.cn/20201006122404662.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3JlbndlaXlpMTQ4Nw==,size_16,color_FFFFFF,t_70#pic_center)
回溯
class Solution {
private List<List<Integer>> ans = new LinkedList<>();
public List<List<Integer>> permute(int[] nums) {
backTrace(nums, 0);
return ans;
}
private void backTrace(int[] nums, int begin) {
if (begin == nums.length) {
List<Integer> one = new LinkedList<>();
for (int num : nums) {
one.add(num);
}
ans.add(one);
}
for (int i = begin; i < nums.length; i++) {
exch(nums, i, begin);
backTrace(nums, begin + 1);
exch(nums, i, begin);
}
}
private void exch(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}