-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-46-Permutations.java
89 lines (73 loc) · 2.69 KB
/
LeetCode-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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
LeetCode: https://leetcode.com/problems/permutations/
LintCode: http://www.lintcode.com/problem/permutations/
JiuZhang: http://www.jiuzhang.com/solutions/permutations/
ProgramCreek: http://www.programcreek.com/2013/02/leetcode-permutations-java/
Analysis:
1.DFS(recursive)
2.BFS(itertive)
*/
class Solution {
// 1.DFS
/*
Runtime: 1 ms, faster than 99.09% of Java online submissions for Permutations.
Memory Usage: 37.2 MB, less than 96.38% of Java online submissions for Permutations.
*/
// public List<List<Integer>> permute(int[] nums) {
// List<List<Integer>> res = new ArrayList<List<Integer>>();
// if(nums == null || nums.length == 0) return res;
// backtrack(nums, res, new ArrayList<>());
// return res;
// }
// private void backtrack(int[] nums, List<List<Integer>> res, List<Integer> list) {
// if (list.size() == nums.length) {
// res.add(new ArrayList<>(list));
// return;
// }
// for (int i = 0; i < nums.length; i++) {
// if (list.contains(nums[i])) continue;
// list.add(nums[i]);
// backtrack(nums, res, list);
// list.remove(list.size() - 1);
// }
// }
// 2.BFS
/*
https://leetcode.com/problems/permutations/discuss/18237/My-AC-simple-iterative-javapython-solution
Analysis
TimeComplexity: O(N!) it's N's factorial time complexity
Insert 1:
[1]
Insert 2:
[2, 1]
[1, 2]
Insert 3:
[3, 2, 1]
[3, 1, 2]
[2, 3, 1]
[1, 3, 2]
[2, 1, 3]
[1, 2, 3]
*/
public List<List<Integer>> permute(int[] nums) {
if(nums == null || nums.length == 0) return null;
List<List<Integer>> result = new ArrayList<List<Integer>>();
result.add(new ArrayList<Integer>());
// Gradually insert the numbers to the result row by row
for (int i = 0; i < nums.length; i++) {
// start insertion of the first row
List<List<Integer>> tempResult = new ArrayList<List<Integer>>();
for (int insertPos = 0; insertPos <= i; insertPos++) {
// the insertPos could be at the beginning of the existing list, it could also be the end of the existing list,
// so the insertPos could be "i"
for (List<Integer> sub : result) {
List<Integer> row = new ArrayList<>(sub);
row.add(insertPos, nums[i]);
tempResult.add(row);
}
}
result = tempResult;
}
return result;
}
}