给定一个数字列表,返回其所有可能的排列。
public class Solution { /* * @param nums: A list of integers. * @return: A list of permutations. */ public List<List<Integer>> permute(int[] nums) { // write your code here List<List<Integer>> list = new ArrayList<>(); if (nums == null) { return null; } if (nums.length == 0) { list.add(new ArrayList<Integer>()); return list; } List<Integer> tmplist = new ArrayList<>(); DFS(list, tmplist, nums); return list; } public void DFS(List<List<Integer>> list, List<Integer> tmplist, int[] nums) { if (tmplist.size() == nums.length) { list.add(new ArrayList<>(tmplist)); return; } for (int i = 0; i < nums.length; i++) { if (tmplist.contains(nums[i])) { continue; } tmplist.add(nums[i]); DFS(list, tmplist, nums); tmplist.remove(tmplist.size()-1); } } }