Leetcode 40 组合总和 II

在这里插入图片描述
思路来自代码随想录

在这里插入图片描述

class Solution:
    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
        paths = []
        path = []
        used = [0] * len(candidates)
        def backtrack(candidates, target, sum_, start_index):
            # Base Case
            if sum_ == target:
                paths.append(path[:])
                return
            # if sum_ > target:
            #     return 
            for i in range(start_index, len(candidates)):
                #剪枝
                if sum_ + candidates[i] > target: 
                    return 
                
                if not used[i]:
                    if i>0 and candidates[i] == candidates[i-1] and not used[i-1]:
                        continue
                used[i] = 1
                sum_ += candidates[i]
                path.append(candidates[i])
                backtrack(candidates, target, sum_, i+1)
                sum_ -= candidates[i]   # 回溯
                path.pop()        # 回溯
                used[i] = 0
        candidates.sort()#剪枝
        backtrack(candidates, target, 0, 0)
        return paths

也可以不维护used数组,只需要修改条件为

if i>start_index and candidates[i] == candidates[i-1]:

以图中第一层搜索第二个节点1为例,第一个1所有的组合已经检索完毕并回溯,现在进入到整个程序第一个for循环中i=1的时候,而start_index是主函数中传入的初始值0(这里和第二层第一个节点1有很大不同是,后者是调用的第二个backtrack函数,传入的start_index是1)

class Solution:
    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
        paths = []
        path = []
        # used = [0] * len(candidates)
        def backtrack(candidates, target, sum_, start_index):
            # Base Case
            if sum_ == target:
                paths.append(path[:])
                return
            # if sum_ > target:
            #     return 
            for i in range(start_index, len(candidates)):
                #剪枝
                if sum_ + candidates[i] > target: 
                    return 
                
                # if not used[i]:
                if i>start_index and candidates[i] == candidates[i-1]:
                    continue
                # used[i] = 1
                sum_ += candidates[i]
                path.append(candidates[i])
                backtrack(candidates, target, sum_, i+1)
                sum_ -= candidates[i]   # 回溯
                path.pop()        # 回溯
                # used[i] = 0
        candidates.sort()#剪枝
        backtrack(candidates, target, 0, 0)
        return paths
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LeetCode 是一个常用的编程学习和面试准备平台,而人工智能 (AI) 处理器组合指的是在处理 LeetCode 题目时,使用多个不同类型的 AI 处理器来提高算法性能和效率。 首先,AI 处理器是具有强大计算能力和优化算法的专用芯片。与传统的通用处理器相比,AI 处理器在处理大量数据和复杂计算任务时表现更佳。 AI 处理器组合意味着将不同类型的 AI 处理器结合使用,以便在编程和解决 LeetCode 题目时获得更好的性能。这些处理器可以包括图像处理单元 (GPU),张量处理单元 (TPU),以及专门针对特定AI应用开发的ASIC等。 在处理 LeetCode 题目时,AI 处理器组合可以带来多个好处。首先,AI 处理器具有并行计算能力,能够同时处理多个任务,从而加快算法的执行速度。其次,AI 处理器通过优化算法和模型,可以提高解题的准确性和效率。例如,对于一些图像和语音处理相关的题目,使用 GPU 或 TPU 可以加速图像和声音的处理,从而更快地得到正确答案。 另外,AI 处理器组合还可以提供更多的资源和内存,以支持处理大规模数据和复杂问题。这对于一些需要使用深度学习或机器学习算法的题目非常重要,因为这些算法通常需要更多的计算资源和存储空间。 综上所述,AI 处理器组合对于处理 LeetCode 题目具有巨大的潜力。它可以加快算法的执行速度,提高解题的准确性和效率,并支持处理大规模数据和复杂问题。然而,组合不同类型的 AI 处理器需要适当的编程技巧和技术知识,以便充分发挥它们的优势和性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值