forked from mehul-1607/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_46.java
32 lines (27 loc) · 1.01 KB
/
_46.java
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
32
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _46 {
public static class Solution1 {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList();
result.add(new ArrayList<>());
return backtracking(nums, 0, result);
}
private List<List<Integer>> backtracking(int[] nums, int index, List<List<Integer>> result) {
if (index == nums.length) {
return result;
}
List<List<Integer>> newResult = new ArrayList<>();
for (List<Integer> eachList : result) {
for (int i = 0; i <= eachList.size(); i++) {
List<Integer> newList = new ArrayList<>(eachList);
newList.add(i, nums[index]);
newResult.add(newList);
}
}
result = newResult;
return backtracking(nums, index + 1, result);
}
}
}