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>> permute(int[] num) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
res.add(new ArrayList<Integer>());
for(int n : num) {
ArrayList<ArrayList<Integer>> cur = new ArrayList<ArrayList<Integer>>();
for(ArrayList<Integer> tmp : res) {
for(int j=0;j<=tmp.size();j++) {
ArrayList<Integer> tmp1 = new ArrayList<Integer>(tmp);
tmp1.add(j, n);
cur.add(tmp1);
}
}
res = new ArrayList<ArrayList<Integer>>(cur);
}
return res;
}
}
没有评论:
发表评论