-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
29 lines (25 loc) · 755 Bytes
/
Copy pathSolution.java
File metadata and controls
29 lines (25 loc) · 755 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
import java.util.ArrayList;
import java.util.List;
class Solution {
private List<List<Integer>> res;
//Time Complexity: (n!)
//Space Complexity: O(n)
public List<List<Integer>> permute(int[] nums) {
res = new ArrayList<>();
if (nums.length == 0) return res;
permute(nums, 0, new ArrayList<>());
return res;
}
private void permute(int nums[], int index, ArrayList<Integer> p){
if (index == nums.length){
res.add(new ArrayList<>(p));
return;
}
for (int i = 0; i < nums.length; i++) {
if (p.contains(nums[i])) continue;
p.add(nums[i]);
permute(nums, index + 1, p);
p.remove(p.size() - 1);
}
}
}