-
Notifications
You must be signed in to change notification settings - Fork 21
/
_0046_Permutations.java
159 lines (133 loc) · 3.74 KB
/
_0046_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package leetcode;
import java.util.*;
/**
* @No 46
* @problem Permutations
* @level Medium
* @desc 全排列
* @author liyazhou1
* @date 2019/09/17 08:09 371公交
*
* <pre>
* Given a collection of distinct integers, return all possible permutations.
*
* Example:
* Input: [1,2,3]
* Output:
* [
* [1,2,3],
* [1,3,2],
* [2,1,3],
* [2,3,1],
* [3,1,2],
* [3,2,1]
* ]
* </pre>
*/
public class _0046_Permutations {
/**
* Note
*
* Thought
* 回溯法
* 递归 + 重置状态 + 剪枝
* execute
* backtrack
* reset
*
* 递归回溯的过程,是一棵树
*
* 全排列是一个基础算法,可以解决"括号生成"、"电话号码的字母组合"等问题
*
* Challenge
*
* Algorithm
* 全排列
*
*/
private static class Solution1 {
List<List<Integer>> result = new ArrayList<>();
public List<List<Integer>> premute(int[] nums) {
if (nums == null || nums.length == 0) {
return Collections.emptyList();
}
backtrack(nums, new Stack<>(), new HashSet<>());
return result;
}
private void backtrack(int[] nums, Stack<Integer> path, Set<Integer> set) {
if (path.size() == nums.length) {
result.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i ++) {
Integer curr = nums[i];
if (set.contains(curr)) {
continue;
}
path.push(curr);
set.add(curr);
backtrack(nums, path, set);
path.pop();
set.remove(curr);
}
}
public static void main(String[] args) {
int[] nums = {1, 2, 3};
List<List<Integer>> result = new Solution1().premute(nums);
for (List<Integer> solution : result) {
System.out.println(solution);
}
}
}
/**
* Note
*
* Thought
* 回溯法
* 递归 + 重置状态 + 剪枝
* execute
* backtrack
* reset
*
* 递归回溯的过程,是一棵树
*
* 全排列是一个基础算法,可以解决"括号生成"、"电话号码的字母组合"等问题
*
* Challenge
*
* Algorithm
* 全排列
* 固定前i个元素
* 使i+2~n索引对应的元素分别于i+1交换位置,后固定i+1个元素
*
* Complexity
* Time,
* Space,
*/
private static class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
if (nums == null || nums.length == 0) {
return result;
}
// List<Integer> seq = Arrays.asList(nums);
List<Integer> seq = new ArrayList<>();
for (int num : nums) {
seq.add(num);
}
permute(result, seq, 0);
return result;
}
private void permute(List<List<Integer>> result, List<Integer> seq, Integer depth) {
if (depth == seq.size()) {
result.add(new ArrayList(seq));
return;
}
for (int i = depth; i < seq.size(); i ++) {
Collections.swap(seq, i, depth);
permute(result, seq, depth+1);
Collections.swap(seq, i, depth);
}
}
}
}