forked from chuyi2007/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.java
More file actions
28 lines (28 loc) · 1.07 KB
/
Permutations.java
File metadata and controls
28 lines (28 loc) · 1.07 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
public class Solution {
public ArrayList<ArrayList<Integer>> permute(int[] num) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> result = new ArrayList<Integer>();
boolean[] set = new boolean[num.length];
backTracking(results, result, num, set, 0);
return results;
}
public void backTracking(ArrayList<ArrayList<Integer>> results,
ArrayList<Integer> result, int[] num, boolean[] set, int count){
if(count == num.length){
results.add(new ArrayList<Integer>(result));
}
else{
for(int i = 0; i < num.length; ++i){
if(!set[i]){
set[i] = true;
result.add(num[i]);
backTracking(results, result, num, set, count + 1);
result.remove(result.size() - 1);
set[i] = false;
}
}
}
}
}