# My Java solution, 4ms, with additional space cost

• public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> numbers = new ArrayList<Integer>();
for(int i=0; i<nums.length; i++){
}
dfs(res, nums.length, numbers, new ArrayList<Integer>());
return res;
}

public void dfs(List<List<Integer>> res, int length, List<Integer> numList, List<Integer> list){
if(list.size()==length){
return;
}
for(int i=0; i<numList.size(); i++){
List<Integer> newList = new ArrayList<>(list);
List<Integer> newNumList = new ArrayList<>(numList);
newNumList.remove(i);
dfs(res, length, newNumList, newList);
}

}
}

• @aixinjueluoqi
My solution to Permutations II

public class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> numbers = new ArrayList<Integer>();
Arrays.sort(nums);
for(int i=0; i<nums.length; i++){
}
dfs(res, nums.length, numbers, new ArrayList<Integer>());
return res;
}

public void dfs(List<List<Integer>> res, int length, List<Integer> numList, List<Integer> list){
if(list.size()==length){
return;
}
for(int i=0; i<numList.size(); i++){
if(i!=0 && numList.get(i)==numList.get(i-1)) continue;
List<Integer> newList = new ArrayList<>(list);