description |
---|
Back-tracking |
- Medium
- Given an array
nums
of distinct integers, return all the possible permutations. You can return the answer in any order.
This is also a back-tracking problem.
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
ans=[]
answer=[]
self.helper(nums,ans,answer)
return answer
def helper(self,candidates, ans,answer):
if not candidates:
answer.append(ans)
return
for i in candidates:
temp=candidates.copy()
temp.remove(i)
#print(ans)
self.helper(temp,ans+[i],answer)