public List<List<Integer>> permute(int[] nums) { List<List<Integer>> results = new ArrayList<>(); permute(nums, 0, results); return results; } public void permute(int[] array, int start, List<List<Integer>> results) { int length = array.length; if (start >= length) { results.add(toIntList(array)); } else { for (int i = start; i < length; i++) { swap(array, start, i); permute(array, start + 1, results); swap(array, start, i); } } } private List<Integer> toIntList(int[] array) { int length = array.length; List<Integer> list = new ArrayList<>(length); for (int i = 0; i < length; i++) { list.add(array[i]); } return list; } public void swap(int[] array, int x, int y) { int temp = array[x]; array[x] = array[y]; array[y] = temp; }