public class match {
public static void main(String[] args) {
Solution666 s=new Solution666();
int[] i=new int[]{1,2,3};
s.permute(i);
}
}
class Solution666 {
private List<Integer> list=new ArrayList<>();
private List<List<Integer>> res=new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
if(nums.length==0){
return res;
}
dfs(nums,0);
return res;
}
public void dfs(int[] nums, int depth) {
if(depth==nums.length){
System.out.println(new ArrayList<>(list));
res.add(new ArrayList<>(list));//一定要使用new
return;
}
for(int i:nums){
//剪枝
/*if(list.contains(i)){
continue;
}*/
list.add(i);
dfs(nums,depth+1);
//回溯到上一步撤销选择
list.remove(depth);
}
}
}
决策树
111执行完会到递归最后执行的先执行撤销选择,再执行for里面第二层循环