Given a collection of distinct numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
permutation(result, new ArrayList<Integer>(), nums);
return result;
}

public void permutation(List<List<Integer>> result, ArrayList<Integer> cur, int[] nums) {
if (cur.size() == nums.length) {
result.add(new ArrayList<Integer>(cur));
} else {
for (int i = 0; i < nums.length; i++) {
if (cur.contains(nums[i])) continue;
cur.add(nums[i]);
permutation(result, cur, nums);
cur.remove(cur.size() - 1);
}
}
}
}