# Share my two C++ solutions,easy to understand

• (1)

``````class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> ret;
int n = nums.size();
if (n == 0)
return ret;

if (n == 1)
{
ret.push_back(nums);
return ret;
}

int i = 0, j = 0, len = 0;
vector<int> curNums;
vector<int> per;
vector<vector<int>> temp;

for (i = 0; i < n; i++)
{
curNums = nums;
curNums.erase(curNums.begin()+i);
temp = permute(curNums);
len = temp.size();

for (j = 0; j < len; j++)
{
per = temp[j];
per.push_back(nums[i]);
ret.push_back(per);
}
}

return ret;
}

};
``````

(2)

``````class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> ret;
vector<int> temp;
int n = nums.size();
if (n == 0)
return ret;

permute_help(nums, 0, n, temp, ret);

return ret;
}

void permute_help(vector<int>& nums, int start, int end, vector<int> temp, vector<vector<int>> &ret)
{
if (start == end)
{
ret.push_back(temp);
return;
}

int i = 0;
int t = 0;

for (i = start; i < end; i++)
{
t = nums[start];
nums[start] = nums[i];
nums[i] = t;

temp.push_back(nums[start]);
permute_help(nums, start+1, end, temp, ret);
temp.pop_back();

t = nums[start];
nums[start] = nums[i];
nums[i] = t;
}
}
};``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.