首页 > 代码库 > 【LeetCode】Permutations

【LeetCode】Permutations

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

 

解法一:递归

class Solution {public:    vector<vector<int> > permute(vector<int> &num)     {        vector<vector<int> > result;        vector<int> cur = num;        Helper(result, cur, 0);        return result;    }    void Helper(vector<vector<int> >& result, vector<int> cur, int start)    {        if(start == cur.size()-1)            result.push_back(cur);        else        {            for(int i = start; i < cur.size(); i ++)            {                swap(cur[start], cur[i]);                Helper(result, cur, start+1);                swap(cur[i], cur[start]);            }        }    }};

【LeetCode】Permutations