1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
public class Permutations { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> result = new ArrayList<List<Integer>>(); if (nums == null || nums.length == 0) { return result; } helper(nums, result, new ArrayList<Integer>()); return result; } private void helper(int[] nums, List<List<Integer>> result, List<Integer> crt) { if (crt.size() >= nums.length) { result.add(new ArrayList<Integer>(crt)); return; } for (int num : nums) { if (!crt.contains(num)) { crt.add(num); helper(nums, result, crt); crt.remove(crt.size() - 1); } } } }
|