题目描述

给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的数字可以无限制重复被选取。

难度

中等

题解

从给定范围内寻求所有可行解,可以使用搜索回溯来解决。

题中已说明candidates中的元素可以无限制重复使用,那么最直观的方法就是直接for循环遍历一遍,看能不能找到两个元素构成target,不行的话再来一遍,看能不能找的三个元素构成target,直到执行了candidates.size()for循环。这时候,递归可以解决这样的多层for循环

伪代码如下:

dfs ()
{
if target == 0
{
保存结果;
返回;
}
for(auto num : candidates)
{
元素存入缓存
dfs()
弹出缓存中最后一个元素,继续
}
}

实现

cpp

class Solution
{
public:
vector<vector<int>> combinationSum(vector<int> &candidates, int target)
{
vector<vector<int>> ans;
vector<int> res;
dfs(ans, res, candidates, target, 0);
return ans;
} void dfs(vector<vector<int>> &ans, vector<int> &res, vector<int> &candidates, int target, int index)
{
if (target == 0)
{
ans.push_back(res);
return;
}
if (target < 0)
return;
//
for (int i = index; i < candidates.size(); ++i)
{
res.push_back(candidates[i]);
dfs(ans, res, candidates, target - candidates[i], i); // 不使用i+1,是为了可以重复使用当前的值
res.pop_back();
}
}
};

golang

改编自cpp实现:

var ans [][]int
var res [] int func combinationSum(candidates []int, target int) [][]int {
ans = make([][]int,0)
res = make([]int,0)
dfs(candidates,target,0)
return ans
} func dfs(candidates []int,target,index int){
if target == 0{
tmp := make([]int,len(res))
copy(tmp,res)
ans = append(ans,tmp)
return
}
if target < 0{
return
}
for i := index;i<len(candidates);i++{
res = append(res,candidates[i])
dfs(candidates,target-candidates[i],i)
res = res[:len(res)-1]
}
}

声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)进行许可,使用时请注明出处。


[LeetCode刷题记录]39 组合总和的更多相关文章

  1. leetcode刷题记录--js

    leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...

  2. Leetcode刷题记录(python3)

    Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...

  3. leetcode 刷题记录(java)-持续更新

    最新更新时间 11:22:29 8. String to Integer (atoi) public static int myAtoi(String str) { // 1字符串非空判断 " ...

  4. LeetCode刷题记录(python3)

    由于之前对算法题接触不多,因此暂时只做easy和medium难度的题. 看完了<算法(第四版)>后重新开始刷LeetCode了,这次决定按topic来刷题,有一个大致的方向.有些题不止包含 ...

  5. LeetCode 刷题记录(二)

    写在前面:因为要准备面试,开始了在[LeetCode]上刷题的历程.LeetCode上一共有大约150道题目,本文记录我在<http://oj.leetcode.com>上AC的所有题目, ...

  6. LeetCode 刷题记录

    写在前面:因为要准备面试,开始了在[LeetCode]上刷题的历程.LeetCode上一共有大约150道题目,本文记录我在<http://oj.leetcode.com>上AC的所有题目, ...

  7. Leetcode题库——39.组合总和

    @author: ZZQ @software: PyCharm @file: combinationSum.py @time: 2018/11/14 18:23 要求:给定一个无重复元素的数组 can ...

  8. leetcode刷题记录——树

    递归 104.二叉树的最大深度 /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree ...

  9. leetCode刷题记录

    (1)Linked List Cycle Total Accepted: 13297 Total Submissions: 38411 Given a linked list, determine i ...

  10. 算法进阶之Leetcode刷题记录

    目录 引言 题目 1.两数之和 题目 解题笔记 7.反转整数 题目 解题笔记 9.回文数 题目 解题笔记 13.罗马数字转整数 题目 解题笔记 14.最长公共前缀 题目 解题笔记 20.有效的括号 题 ...

随机推荐

  1. docker镜像列表存在但删除显示 No such image问题解决

    近期使用了docker,但删除镜像时候遇到了无法删除问题.提示:No such Image.原因有两个,解决方法如下: 原因1: 容器还存在是无法删除镜像的 解决步骤: 1.停掉容器(docker s ...

  2. PPT 常规设置

    高级设置 可以将撤销次数调大,最多 150次 默认拉到PPT中的图片是被压缩的,可以设置成不压缩(解压 PPT 可查看里面的图片大小) 字体嵌入 可将自动保存时间调短,默认保存目录我习惯先保存到桌面( ...

  3. zsh踩坑记录

    1. zsh: no matches found: uvicorn[standard] 方法一 # 在~/.zshrc中添加下面这句话 setopt no_nomatch # 然后source ~/. ...

  4. WCF 使用动态代理精简代码架构 (WCF动态调用)

    使用Castle.Core.dll实现,核心代码是使用Castle.DynamicProxy.ProxyGenerator类的CreateInterfaceProxyWithoutTarget方法动态 ...

  5. 【AcWing】第6场周赛 B题 3734. 求和 (思维)

    AcWing 3734. 求和 其实这道题并不难,只是思维性很强! 因为 \(a\) 的各个数位不包含除了 \(4\) 和 \(7\)​ 以外的其他数字. 仔细观察数据会发现因为 \(1\le l \ ...

  6. 【每日一题】11.黑白树 (树上DFS)

    补题链接:Here 题目描述 一棵 \(n\) 个点的有根树,\(1\) 号点为根,相邻的两个节点之间的距离为 \(1\) .树上每个节点 \(i\)对应一个值\(k[i]\).每个点都有一个颜色,初 ...

  7. 涂色游戏Flood-it!(IDA star算法) - HDU 4127

    做题之前,可以先到下面这个网站玩一会游戏: https://unixpapa.com/floodit/?sz=14&nc=6 游戏开发里面,比较常用的一个搜索算法是寻路算法,寻路算法里面用的最 ...

  8. Vue+Element前端导入导出Excel

    1 <el-upload 2 class="upload-demo" 3 :action="uploadUrl()" 4 :limit="1&q ...

  9. socket TCP DPT 网络编程

    复习: ARP协议: 广播和单播 通过ip地址获得mac地址 机器A发起一个arp请求(只包含A的ip地址) 交换机接收到请求,广播这条消息 所有的机器都会接受到这条请求,只有需要寻找的机器B的ip地 ...

  10. 如何使用单纯的`WebAssembly`

    一般来说在.net core使用WebAssembly 都是Blazor ,但是Blazor渲染界面,.net core也提供单纯的WebAssembly这篇博客我将讲解如何使用单纯的WebAssem ...