-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution0046.java
More file actions
43 lines (37 loc) · 1.21 KB
/
Solution0046.java
File metadata and controls
43 lines (37 loc) · 1.21 KB
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
package leetcode;
import java.util.ArrayList;
import java.util.List;
/**
* @author xiangdotzhaoAtwoqutechcommacom
* @date 2019/12/13
*/
public class Solution0046 {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
boolean[] used = new boolean[nums.length];
if (nums.length == 0) {
return res;
}
generatePermutation(nums, 0, new ArrayList<>(), res, used);
return res;
}
public void generatePermutation(int[] nums, int pos, List<Integer> tempList, List<List<Integer>> res, boolean[] used) {
if (pos == nums.length) {
res.add(new ArrayList<>(tempList));
return;
}
for (int i = 0; i < nums.length; i++) {
if (!used[i]) {
tempList.add(nums[i]);
used[i] = true;
generatePermutation(nums, pos + 1, tempList, res, used);
tempList.remove(tempList.size() - 1);
used[i] = false;
}
}
}
public static void main(String[] args) {
List<List<Integer>> permute = new Solution0046().permute(new int[]{1, 1, 2});
System.out.println(permute);
}
}