-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path46-permutations.java
32 lines (30 loc) · 1.12 KB
/
46-permutations.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
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
dfs(nums, new ArrayList<>(), new HashSet<>(), res);
return res;
}
public void dfs(int[] nums, ArrayList<Integer> path, HashSet<Integer> visited, List<List<Integer>> res) {
if (path.size() == nums.length) {
res.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
if (!visited.contains(i)) {
path.add(nums[i]);
visited.add(i);
dfs(nums, path, visited, res);
path.remove(path.size() - 1);
visited.remove(i);
}
}
}
}
// time O(n*n!), dfs will calls n! times and each non-leaf node traverse list costs O(n) and leaf node copy a list to answer costs O(n)
// space O(n*n!), because answer contains n! permutations and each permutaiton can cost O(n). Besides, memory stack size is O(n)
// using dfs and backtracking and permutation
/*
1. type: permutation
2. duplicate elements: no
3. selectable repeatedly: no
*/