[LeetCode刷题记录]39 组合总和
题目描述
给定一个无重复元素的数组 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 组合总和的更多相关文章
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- Leetcode刷题记录(python3)
Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...
- leetcode 刷题记录(java)-持续更新
最新更新时间 11:22:29 8. String to Integer (atoi) public static int myAtoi(String str) { // 1字符串非空判断 " ...
- LeetCode刷题记录(python3)
由于之前对算法题接触不多,因此暂时只做easy和medium难度的题. 看完了<算法(第四版)>后重新开始刷LeetCode了,这次决定按topic来刷题,有一个大致的方向.有些题不止包含 ...
- LeetCode 刷题记录(二)
写在前面:因为要准备面试,开始了在[LeetCode]上刷题的历程.LeetCode上一共有大约150道题目,本文记录我在<http://oj.leetcode.com>上AC的所有题目, ...
- LeetCode 刷题记录
写在前面:因为要准备面试,开始了在[LeetCode]上刷题的历程.LeetCode上一共有大约150道题目,本文记录我在<http://oj.leetcode.com>上AC的所有题目, ...
- Leetcode题库——39.组合总和
@author: ZZQ @software: PyCharm @file: combinationSum.py @time: 2018/11/14 18:23 要求:给定一个无重复元素的数组 can ...
- leetcode刷题记录——树
递归 104.二叉树的最大深度 /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree ...
- leetCode刷题记录
(1)Linked List Cycle Total Accepted: 13297 Total Submissions: 38411 Given a linked list, determine i ...
- 算法进阶之Leetcode刷题记录
目录 引言 题目 1.两数之和 题目 解题笔记 7.反转整数 题目 解题笔记 9.回文数 题目 解题笔记 13.罗马数字转整数 题目 解题笔记 14.最长公共前缀 题目 解题笔记 20.有效的括号 题 ...
随机推荐
- byte[] 数组,创建的时候赋初始值
C# //创建一个长度为10的byte数组,并且其中每个byte的值为0x08. byte[] myByteArray = Enumerable.Repeat((byte)0x08, 10).ToAr ...
- Sublime Text 16进制显示
大文件推荐使用 UltraEdit 工具 Sublime Text 16进制显示(可以直接显示不同数据类型转换后的结果,不用在线工具,转二进制了) 安装 HexViewer 插件 1. Ctrl + ...
- SpringBoot Admin OFFLINE
java.util.concurrent.TimeoutException message Did not observe any item or terminal signal within 100 ...
- python 读取数据调翻译更新表字段
import time import requests import pymysql import datetime import random from hashlib import md5 imp ...
- 面试官:分库分表后如何生成全局ID?
分库分表后就不能使用自增 ID 来作为表的主键了,因为数据库自增 ID 只适用于单机环境,但如果是分布式环境,是将数据库进行分库.分表或数据库分片等操作时,那么数据库自增 ID 就会生成重复 ID,从 ...
- C++ Idioms Pimpl
References C++ Coding Standard 這本書的中文版不知道是不是翻譯問題,還是原作就有這種傾向,有些咬文嚼字的很不好懂. Exceptional C++ 這本比上面那本容易理解 ...
- 第六届蓝桥杯C++A组 A~F题题解
蓝桥杯历年国赛真题汇总:Here 1.方格填数 在2行5列的格子中填入1到10的数字. 要求: 相邻的格子中的数,右边的大于左边的,下边的大于上边的. 如[图1.png]所示的2种,就是合格的填法. ...
- Redis无法使用ip访问
问题: 启动redis服务,可以使用127.0.0.1配置并使用访问redis,但是换成IP地址就无法访问 127.0.0.1 可以 [root]# ./redis-cli -c -h 127 ...
- vue项目使用el-table实现无限滚动
https://blog.csdn.net/weixin_44994731/article/details/107980827 1.安装el-table-infinite-scroll yarn ad ...
- python常见面试题讲解(七)合并表记录
题目描述 数据表记录包含表索引和数值(int范围的整数),请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出. 输入描述: 先输入键值对的个数然后输入成对的in ...