代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可

@param num int整型一维数组

@param target int整型

@return int整型二维数组

import json

class Solution: # def init(self): # self.res = set()

def combinationSum2(self, num: List[int], target: int) -> List[List[int]]:
    # write code here
    """
    回溯法
    1.去重(好好理解一下)
    2.剪枝(不剪枝会超时)
    """
    result = []
    if not num:
        return result
    new_num = sorted(num)
    self.backtracking(new_num, target, 0, 0, [], result)
    return result

def backtracking(self, num, target, cur, begin, arr, result):
    """
    num: 入参数组列表
    target:目标值
    cur:当前值
    begin:开始指针
    arr:临时存储数组
    result:满足条件的组合
    """
    if cur >= target:
        if cur == target:
            result.append(list(arr))
        return result
    for i in range(begin, len(num)):
        if i > begin and num[i] == num[i - 1]:   # 去重
            continue
        # 减枝
        arr.append(num[i])
        self.backtracking(num, target, cur + num[i], i + 1, arr, result)
        arr.pop(-1)
    return result