-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations0046.java
More file actions
60 lines (50 loc) · 1.44 KB
/
Permutations0046.java
File metadata and controls
60 lines (50 loc) · 1.44 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.naming.spi.DirStateFactory.Result;
/**
* È«ÅÅÁÐ
*/
public class Permutations0046{
public static void main(String[] args) {
int nums[] = {1, 2, 3};
permute(nums);
for (List<Integer> l : result) {
for (int i : l) {
System.out.print(i);
}
System.out.println();
}
}
// ===========
static List<List<Integer>> result = new ArrayList<List<Integer>>();
static boolean flag[];
public static List<List<Integer>> permute(int[] nums) {
result = new ArrayList<List<Integer>>();
flag = new boolean[nums.length];
for (int i = 0; i < flag.length; i++) {
flag[i] = true;
}
int tmp[] = new int[nums.length];
backtracking(nums, 0, tmp);
return result;
}
private static void backtracking(int nums[], int start, int[] tmp) {
if (start >= nums.length) {
List<Integer> tlist = new ArrayList<>();
for (int i : tmp) {
tlist.add(i);
}
result.add(tlist);
return;
}
for (int i = 0; i < nums.length; i++) {
if (flag[i]) {
flag[i] = false;
tmp[i] = nums[start];
backtracking(nums, start+1, tmp);
flag[i] = true;
}
}
}
}