public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
res.add(new ArrayList<Integer>());
if (nums == null || nums.length == 0) {
return res;
}
for (int i = 0; i < nums.length; i++) {
List<List<Integer>> oneRound = new ArrayList<>();
for (List<Integer> cur : res) {
// because you need to insert from front to end so + 1
for (int j = 0; j < cur.size() + 1; j++) {
cur.add(j, nums[i]);
List<Integer> tmp = new ArrayList<Integer>(cur);
oneRound.add(tmp);
cur.remove(j);
}
}
res = oneRound;
}
return res;
}