[leetcode] Permutations

Permutations

思路:交换+DFS

class Solution {
    vector<vector<int> > res;
    int length;
    
public:
    vector<vector<int> > permute(vector<int> &num) {
        length=num.size();//有length个数
        res.clear();
        perm(num,0);
        return res;
    }
    void perm(vector<int> &num,int i){
        if (i==length) {
            res.push_back(num);
        }
        for (int j=i; j<length; j++) {
            swap(num[i], num[j]);
            perm(num, i+1);
            swap(num[j], num[i]);
        }
        
    }
};

Reference:

permutations的讨论

你可能感兴趣的:([leetcode] Permutations)