给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的数字可以无限制重复被选取。
说明:
所有数字(包括 target)都是正整数。
解集不能包含重复的组合。

示例 1:
输入: candidates = [2,3,6,7], target = 7,
所求解集为:
[
[7],
[2,2,3]
]

示例 2:
输入: candidates = [2,3,5], target = 8,
所求解集为:
[
[2,2,2,2],
[2,3,3],
[3,5]
]

#include <iostream>
#include <vector>
using namespace std;
static int i=; void recurrent(vector<int>& candidates,int target,int addr,vector<vector<int>>& ans,vector<int>& temp)//addr为当前遍历到的位置,
{
if(target==)
{
//cout<<temp.size()<<endl;
ans.push_back(temp);
return;
}
if(target<) return;
for(int i=addr;i<candidates.size();i++)
{
target-=candidates[i];
temp.push_back(candidates[i]);
recurrent(candidates,target,i,ans,temp);
temp.pop_back();//还原,以继续遍历
target+=candidates[i];
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> ans;
int count=candidates.size();
if(count==||target<) return ans;
vector<int> temp;
recurrent(candidates,target,,ans,temp);
return ans;
} int main() {
vector<int> candidates={};
vector<vector<int>> ans=combinationSum(candidates,);
std::cout << ans.size()<< std::endl;
return ;
}

#leetcode刷题之路39-组合总和的更多相关文章

  1. LeetCode刷题笔记-回溯法-组合总和问题

    题目描述: <组合总和问题>给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. cand ...

  2. #leetcode刷题之路40-组合总和 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说 ...

  3. python -- leetcode 刷题之路

    第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...

  4. 使用Java+Kotlin双语言的LeetCode刷题之路(三)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  5. 使用Java+Kotlin双语言的LeetCode刷题之路(二)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  6. 使用Java+Kotlin双语言的LeetCode刷题之路(一)

    LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...

  7. #leetcode刷题之路22-括号生成

    给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为:[ "((()))", "(()())&q ...

  8. #leetcode刷题之路16-最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  9. #leetcode刷题之路13-罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M.字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写 ...

随机推荐

  1. Android View的事件分发机制和滑动冲突解决方案

    这篇文章会先讲Android中View的事件分发机制,然后再介绍Android滑动冲突的形成原因并给出解决方案.因水平有限,讲的不会太过深入,只希望各位看了之后对事件分发机制的流程有个大概的概念,并且 ...

  2. Object equals 方法

    package com.mydemo.controller; public class TestEquals { public static void main(String[] args) { Do ...

  3. geogebra几何画图工具用法

    1. intersectPath :该命令可以自动“算出”对应多边形的交汇区域 2. 静态文本可以指定到一个对象的中间这样将来动态变化对象大小时也不出现问题 3.export worksheet 4. ...

  4. linux克隆机器

    首先你要先点击你的虚拟机点击克隆: 然后执行这个 vim /etc/sysconfig/network-scripts/ifcfg-eth0 去这里修改这两行 然后注释了 然后再执行这个 > / ...

  5. Linux查看系统当前字符集

    常用的命令展示 参考当前环境的字符集 方法一: cat /etc/sysconfig/i18n [ssh客户端工具最好也是utf-8,保持一致] 方法二:echo $LANG 设置当前环境的字符集 方 ...

  6. Linux /dev/null详解

    常用的命令展示 /dev/null 和 /dev/zero的区别        1./dev/null:表示 的是一个黑洞,通常用于丢弃不需要的数据输出, 或者用于输入流的空文件            ...

  7. You may experience an access violation when you access an STL object through a pointer or reference in a different DLL or EXE

    Symptoms When accessing an STL object created in one DLL or EXE through a pointer or reference in a ...

  8. I - Matches Game(异或运算符的使用)

    I - Matches Game Description Here is a simple game. In this game, there are several piles of matches ...

  9. 第2次作业——APP案例分析

    第一部分 调研, 评测 1.下载软件并使用起来,描述最简单直观的个人第一次上手体验. 知乎,中文互联网最大的知识平台.使用知乎这个APP3年了,目睹了它的兴盛(当然没有衰亡@_@).打开这款APP,主 ...

  10. iptables.md

    iptables基本概念 工作流程 1.一个数据包进入网卡时,它首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转发出去. 2.如果数据包就是进入本机的,它就会沿着图向下移动,到达I ...