-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC46_Permutations.java
More file actions
33 lines (28 loc) · 951 Bytes
/
LC46_Permutations.java
File metadata and controls
33 lines (28 loc) · 951 Bytes
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
33
package practise.leetCode;
import java.util.ArrayList;
import java.util.List;
public class LC46_Permutations {
public static void main(String[] args) {
int[] nums = {1, 2, 3};
List<List<Integer>> result = permute(nums);
System.out.println(result);
}
private static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
backtrack(nums, new ArrayList<>(), result);
return result;
}
private static void backtrack(int[] nums, List<Integer> current, List<List<Integer>> result) {
if (current.size() == nums.length) {
result.add(new ArrayList<>(current));
return;
}
for (Integer num : nums) {
if (current.contains(num))
continue;
current.add(num);
backtrack(nums, current, result);
current.remove(current.size() - 1);
}
}
}