三部曲
class solution:
def init(self):
1.确定回溯参数
def backtracking(s,0)
2.确定终止条件
if startIndex >len(s):
3.核心是确定单层遍历条件。
for i in range(startindex, len(s))
leetcode 90. 子集 II
class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
res = []
path = []
nums.sort() # 去重需要先对数组进行排序
def backtracking(nums, startIndex):
# 终止条件
res.append(path[:])
if startIndex == len(nums):
return
for i in range(startIndex, len(nums)):
if i > startIndex and nums[i] == nums[i-1]:
continue
path.append(nums[i])
backtracking(nums, i+1)
path.pop()