Permutations(copy)

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 {
private:
    vector<vector<int>> res;
public:
    void permute_help(vector<int> &nums,int begin) 
    {
        if(begin >= nums.size()) return;
        permute_help(nums, begin+1);
        for(int i=0; i<begin; i++)
        {
            swap(nums[i], nums[begin]);
            res.push_back(nums);
            permute_help(nums, begin+1);
            swap(nums[i], nums[begin]);
        }
    }
    vector<vector<int>> permute(vector<int>& nums) {
        res.push_back(nums);
        permute_help(nums,1);
        return res;
    }
};
posted @ 2015-11-28 22:21  djiankuo  阅读(152)  评论(0编辑  收藏  举报