经典问题,将组合的index标记换为了used来判断元素使用情况,对于全排列的情况,时间复杂度为o(n!),消耗的系统栈为nums长度n,结果数组中存放n!个数据
class Solution {
private:vector<vector<int>> result;vector<int> permulation;void permute(vector<int>&nums,vector<bool>&used){if(permulation.size()==nums.size())result.push_back(permulation);elsefor(int i=0;i<nums.size();i++)if(!used[i]){used[i]=true;permulation.push_back(nums[i]);permute(nums,used);permulation.pop_back();used[i]=false;}}
public:vector<vector<int>> permute(vector<int>& nums) {vector<bool> used(nums.size(),false);permute(nums,used);return result;}
};