java:
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
boolean[] used = new boolean[nums.length];
backTracking(used, nums);
return res;
}
public void backTracking(boolean[] used, int[] nums) {
if (path.size() == nums.length){
res.add(new ArrayList<>(path));
return ;
}
for (int i = 0; i < nums.length; i++) {
if (used[i] == true)
continue;
path.add(nums[i]);
used[i] = true;
backTracking(used, nums);
path.remove(path.size() - 1);
used[i] = false;
}
}
}