Given a list of numbers, return all possible permutations.
class Solution {
/** * @param nums: A list of integers. * @return: A list of permutations. */
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> results = new ArrayList<>();
if (nums == null) {
return results;
}
if (nums.length == 0) {
results.add(new ArrayList<Integer>());
return results;
}
List<Integer> result = new ArrayList<>();
dfsHelper(nums, results, result);
return results;
}
private void dfsHelper(int[] nums,
List<List<Integer>> results,
List<Integer> result) {
if (result.size() == nums.length) {
results.add(new ArrayList<Integer>(result));
}
for (int i = 0; i < nums.length; i++) {
if (result.contains(nums[i])) {
continue;
}
result.add(nums[i]);
dfsHelper(nums, results, result);
result.remove(result.size() - 1);
}
}
}