回溯

import java.util.*;

public class Solution {
    public ArrayList<ArrayList<Integer>> permute(int[] num) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        LinkedList<Integer> temp = new LinkedList<>();
        backTrack(num,temp,res);
        return res; 
    }
    public void backTrack(int[] num,LinkedList<Integer> temp,ArrayList<ArrayList<Integer>> res){
        if(temp.size() == num.length){
            res.add(new ArrayList<>(temp));
            return;
        }
        for(int i = 0;i<num.length;i++){
            if(temp.contains(num[i])){
                continue;
            }
            temp.add(num[i]);
            backTrack(num,temp,res);
            temp.removeLast();
        }
        
    }
}