class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
res = []
def helper(nums, temp):
if not nums:
if temp not in res:
res.append(temp)
for i in range(len(nums)):
helper(nums[:i] + nums[i + 1:], temp + [nums[i]])
helper(nums, [])
return res
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
def helper(path):
if len(path) == l:
res.append(path[:])
for i in range(l):
if not used[i]:
used[i] = True
path.append(nums[i])
helper(path)
path.pop(-1)
used[i] = False
l = len(nums)
res = []
used = [False for _ in nums]
helper([])
return res
class Solution {
public:
void helper(vector<vector<int>>& res, vector<int>& output, int first, int len)
{
if (first == len)
{
res.emplace_back(output);
return ;
}
for(int i = first; i < len; i++)
{
swap(output[first], output[i]);
helper(res, output, first + 1, len);
swap(output[first], output[i]);
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
helper(res, nums, 0, (int)nums.size());
return res;
}
};