LeetCode 46 - Permutations

954 ワード

詳細
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 List> permute(int[] num) {
    List> result = new ArrayList<>();
    perm(result, num, 0);
    return result;
}

public void perm(List> result, int[] num, int pos) {
    if(pos == num.length) {
        List list = new ArrayList();
        for(int a:num) list.add(a);
        result.add(list);
        return;
    }
    for(int i=pos; i