public List<List<Integer>> permute(int[] num) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
helper(num, new boolean[num.length], new ArrayList<Integer>(), res);
return res;
}
private void helper(int[] num, boolean[] used, ArrayList<Integer> curr, List<List<Integer>> res) {
if (curr.size()==num.length) {
res.add(new ArrayList<Integer>(curr));
return;
}
for (int i=0; i<num.length; i++) {
if (!used[i]) {
used[i]=true;
curr.add(num[i]);
helper(num, used, curr, res);
curr.remove(curr.size()-1);
used[i]=false;
}
}
}
No comments:
Post a Comment