题目: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; } };