-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPermutations.java
More file actions
41 lines (31 loc) · 1.27 KB
/
Copy pathPermutations.java
File metadata and controls
41 lines (31 loc) · 1.27 KB
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
34
35
36
37
38
39
40
41
// Given a collection of distinct integers, return all possible permutations.
// See: https://leetcode.com/problems/permutations/
package leetcode.backtracking;
import java.util.LinkedList;
import java.util.List;
public class Permutations {
private List<List<Integer>> result = new LinkedList<List<Integer>>();
public List<List<Integer>> permute(int[] nums) {
genPerm(nums, new boolean[nums.length], new LinkedList<>());
return result;
}
private void genPerm(int[] nums, boolean[] used, LinkedList<Integer> currPerm) {
if (nums.length == currPerm.size()) {
result.add(new LinkedList<>(currPerm));
return;
}
for (int i = 0; i < nums.length; i++)
if (!used[i]) {
currPerm.add(nums[i]);
used[i] = true;
genPerm(nums, used, currPerm);
currPerm.removeLast();
used[i] = false;
}
}
public static void main(String[] args) {
System.out.println(new Permutations().permute(new int[] {1, 2, 3}));
System.out.println(new Permutations().permute(new int[] {1, 2}));
System.out.println(new Permutations().permute(new int[] {1, 2, 3, 4}));
}
}