forked from algorithm008-class02/algorithm008-class02
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermute.java
More file actions
31 lines (24 loc) · 693 Bytes
/
Permute.java
File metadata and controls
31 lines (24 loc) · 693 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
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Permute {
private List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
findPermute(nums, new Stack<>());
return res;
}
public void findPermute(int[] nums, Stack<Integer> stack) {
if (stack.size() == nums.length) {
res.add(stack);
return;
}
for (int i = 0; i < nums.length; i++) {
if (stack.contains(nums[i])) {
continue;
}
stack.push(nums[i]);
findPermute(nums, stack);
stack.pop();
}
}
}