题目:
题目链接:https://leetcode-cn.com/problems/permutations/
解题思路:
回溯 + 剪枝,大概框架为:
def function():
if 满足插入结果条件:
插入结果集
return
elif 不满足继续遍历条件(比如次数等要求达到上限):
return
for ...
感觉好多回溯的题,是时候总结一下回溯的题了
代码实现:
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
def permute_help(nums, path, res):
if 0 == len(nums):
res.append(path[:])
return
for idx in range(len(nums)):
new_nums = nums[:]
new_nums.pop(idx)
path.append(nums[idx])
permute_help(new_nums, path, res)
path.pop()
res = []
permute_help(nums, [], res)
return res