[Leetcode] Permutations

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].

注意for循环的其实条件。

用swap为了方便、简洁。


class Solution {
public:
    vector > permute(vector &num) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() 
        vector answer;
        vector > result;
        dfs(result,answer,num,0);
        return result;        
    }
    
    
    void dfs (vector > &result, vector &answer, vector &num, int depth)
    {
        if(depth==num.size())
        {
            result.push_back(num);
            return;           
        }
        
        for(int i=depth;i


你可能感兴趣的:(Leetcode)