Given a collection of numbers, return all possible permutations.
For example,
[1,2,3]
have the following permutations:
[1,2,3]
, [1,3,2]
, [2,1,3]
, [2,3,1]
, [3,1,2]
, and [3,2,1]
.
public class Solution {
public ArrayList<ArrayList<Integer>> res;
public ArrayList<Integer> list;
public ArrayList<ArrayList<Integer>> permute(int[] num) {
// Start typing your Java solution below
// DO NOT write main() function
res = new ArrayList<ArrayList<Integer>>();
list = new ArrayList<Integer>();
perm(num, 0);
return res;
}
public void perm(int[] num, int cur){
if(cur == num.length){
for(int i = 0; i < num.length; i++)
list.add(num[i]);
res.add(list);
list = new ArrayList<Integer>();
}
for(int i = cur; i < num.length; i++){
int temp = num[i];
num[i] = num[cur];
num[cur] = temp;
perm(num, cur + 1);
temp = num[i];
num[i] = num[cur];
num[cur] = temp;
}
}
}