LeetCode题目:Permutations

题目:Given a collection of distinct numbers, return all possible permutations.

大意:全排列给定数组,其中给定数组中没有相同的元素。

解决方法:分治法

class Solution {
private:
    vector<vector<int>> coll;
    void helper(vector<int> &nums, int p){
        int size = nums.size();
        if(size == p + 1 || 0 == size)
            coll.push_back(nums);
        else{
            for(int i = p; i < size; ++i){
                swap(nums[p], nums[i]);
                helper(nums, p + 1);
                swap(nums[p], nums[i]);
            }
        }
    }
public:
    vector<vector<int>> permute(vector<int>& nums) {
        helper(nums, 0);
        return coll;
    }
};

 

posted @ 2016-03-04 09:15  Runnyu  阅读(155)  评论(0编辑  收藏  举报