题目描述(中等难度)
解题思路
回溯法
python 代码
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
res = []
trace = []
visit = [0]*len(nums)
self.backtrace(nums,trace,res,visit)
return res
def backtrace(self,nums,trace,res,visit):
if len(trace)==len(nums):
res.append(trace[:])
return
for i in range(len(nums)):
if visit[i] == 1:
continue
visit[i] = 1
trace.append(nums[i])
self.backtrace(nums,trace,res,visit)
trace.pop()
visit[i] = 0
s = Solution()
result = s.permute([1,2,3])
print(result) #