首页 > 其他分享 >40. 组合总和 II

40. 组合总和 II

时间:2023-01-10 19:45:46浏览次数:57  
标签:target int res 40 II candidates len path 总和

40. 组合总和 II

难度中等1200

给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用 一次

注意:解集不能包含重复的组合。

示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]

示例 2:

输入: candidates = [2,5,2,1,2], target = 5,
输出:
[
[1,2,2],
[5]
]

提示:

  • 1 <= candidates.length <= 100
  • 1 <= candidates[i] <= 50
  • 1 <= target <= 30

通过次数382,058

提交次数63

方法一:used数组

var(
    path []int
    res [][]int
    used  []bool
)
func combinationSum2(candidates []int, target int) [][]int {
    path,res,used=make([]int,0,len(candidates)),make([][]int,0),make([]bool,len(candidates))
    sort.Ints(candidates)
    dfs(candidates,target,0,0)
    return res
}

func dfs(candidates []int,target int,sum int,startindex int){
    if target==sum{
        tmp:=make([]int,len(path))
        copy(tmp,path)
        res=append(res,tmp)
        return
    }
    if target<sum{
        return
    }
    for i:=startindex;i<len(candidates);i++{
        if candidates[i] > target {  // 剪枝,提前返回
            break
        }
        if i>0 && candidates[i]==candidates[i-1] && used[i-1]==false{//去重
            continue
        }
        sum+=candidates[i]
        path=append(path,candidates[i])
        used[i]=true
        dfs(candidates,target,sum,i+1)
        used[i]=false
        sum-=candidates[i]
        path=path[:len(path)-1]
        
    }

}

方法二:

var (
    res [][]int
    path  []int
)
func combinationSum2(candidates []int, target int) [][]int {
    res, path = make([][]int, 0), make([]int, 0, len(candidates))
    sort.Ints(candidates)   // 排序,为剪枝做准备
    dfs(candidates, 0, target)
    return res
}

func dfs(candidates []int, start int, target int) {
    if target == 0 {   // target 不断减小,如果为0说明达到了目标值
        tmp := make([]int, len(path))
        copy(tmp, path)
        res = append(res, tmp)
        return
    }
    for i := start; i < len(candidates); i++ {
        if candidates[i] > target {  // 剪枝,提前返回
            break
        }
        // i != start 限制了这不对深度遍历到达的此值去重
        if i != start && candidates[i] == candidates[i-1] { // 去重
            continue
        }
        path = append(path, candidates[i])
        dfs(candidates, i+1, target - candidates[i])
        path = path[:len(path) - 1]
    }
}

标签:target,int,res,40,II,candidates,len,path,总和
From: https://www.cnblogs.com/suehoo/p/17041226.html

相关文章

  • 40、商品服务--属性分组---分组新增&级联选择器
    当我们点击新增时,希望是下面这样的形式,即所属分类是下面这样的我们可以使用elementui的级联选择器来实现其中options是显示的数组,所以我们给后端发送请求来查询然后给......
  • liinux-目录、文件结构及相关命令
    1.前期必备知识1.命令提示符[root@max001~]#:root表示用户信息,max001表示主机名称。[root@max001~]%:普通用户结尾是$符号。2.命令格式规范(语法规范) 01.linux中......
  • 部署iis遇到的一些错误
    “不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况“的解决方案#解决方案出现这个错误是因为IIS7采用了更安全的web.config管理机制,默认情......
  • 680. 验证回文串II
    问题链接https://leetcode.cn/problems/valid-palindrome-ii/description/解题思路这题可以用贪心。贪心的思路是,我们假定遇到的第一个不匹配的字符,删掉就是有可能使我......
  • 39. 组合总和
    39.组合总和难度中等2302收藏分享切换为英文接收动态反馈给你一个无重复元素的整数数组candidates和一个目标整数target,找出candidates中可以使数字和为目标数......
  • openEuler RISC-V 的 Firefox 性能大升级,最高 40 倍性能提升
    RISC-VSIG择日即将发布openEulerRISC-V22.03V2版本镜像。本次发版会提供带有SpiderMonkeyJIT编译支持的Firefox最新版本和带有LLVMpipe优化的Mesa最新版本......
  • 国产电源芯片DP4054 软硬件兼容TP4054 规格书资料
    DP4054是一款完整的采用恒定电流/恒定电压单节锂离子电池充电管理芯片。其SOT小封装和较少的外部元件数目使其成为便携式应用的理想器件,DP4054可以适合USB 电源和适配......
  • iis重新安装
    先卸载iis然后删除C:\Windows\System32\inetsrv\config在删除C:\inetpub然后重新安装iis本地ip能打开,127.0.0.1打不开;修改ip参考地址:http://t.zoukankan.com/laoq112-......
  • 迁移学习(IIMT)——《Improve Unsupervised Domain Adaptation with Mixup Training》
    论文信息论文标题:ImproveUnsupervisedDomainAdaptationwithMixupTraining论文作者:ShenYan,HuanSong,NanxiangLi,LincanZou,LiuRen论文来源:arxiv2020论文......
  • 刷刷刷Day8| 剑指 Offer 58 - II. 左旋转字符串
    剑指Offer58-II.左旋转字符串LeetCode题目要求字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比......