-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSolution046.java
48 lines (40 loc) · 1.21 KB
/
Solution046.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
package algorithm.leetcode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* @author: mayuan
* @desc: 全排列
* @date: 2018/08/01
*/
public class Solution046 {
public static void main(String[] args) {
Solution046 sol = new Solution046();
System.out.println(sol.permute(new int[]{1, 2, 3}));
}
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> answer = new ArrayList<>();
if (null == nums || 0 >= nums.length) {
return answer;
}
dfs(answer, new LinkedList(), nums);
return answer;
}
private void dfs(List<List<Integer>> answer, List<Integer> oneAnswer, final int[] nums) {
if (oneAnswer.size() == nums.length) {
answer.add(new ArrayList<>(oneAnswer));
return;
}
for (int i = 0; i < nums.length; ++i) {
if (oneAnswer.contains(nums[i])) {
continue;
}
// 添加当前元素
oneAnswer.add(nums[i]);
dfs(answer, oneAnswer, nums);
// 移除当前元素
oneAnswer.remove(oneAnswer.size() - 1);
}
}
}