博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode]Combination Sum
阅读量:6991 次
发布时间:2019-06-27

本文共 1202 字,大约阅读时间需要 4 分钟。

使用深度优先搜索解决。

#include 
#include
#include
using namespace std;class Solution {public: vector
> combinationSum(vector
&candidates, int target) { vector
tmpresult; tmpresult.clear(); combinationSumCore(candidates, 0, target, tmpresult); return res; } void combinationSumCore(vector
&candidates, int curr, int target, vector
tmpresult){ if (0 == target){ res.push_back(tmpresult); return; } if (curr == candidates.size()) return; for (int i = 0; i <= target/candidates[curr]; i++){ vector
tmpresult1(tmpresult); for (int j = 0; j < i; j++) tmpresult1.push_back(candidates[curr]); combinationSumCore(candidates, curr+1, target-i*candidates[curr], tmpresult1); } }private: vector
> res;};int main(){ vector
vec; vec.push_back(1); Solution s; s.combinationSum(vec, 2); return 0;}

 

 

 

 

 

 

EOF

转载地址:http://befvl.baihongyu.com/

你可能感兴趣的文章
基于CentOS5.5的SVN服务器搭建
查看>>
maven使用笔记
查看>>
JBoss配置使项目能在局域网其他机子上访问项目
查看>>
VIO概述 On-Manifold Preintegration for Real-Time Visual--Inertial Odometry
查看>>
CocoaPods升级安装三方库报错
查看>>
SpringBoot整合RabbitMQ实现微服务间的异步消息沟通
查看>>
pku1338 Ugly Numbers
查看>>
程序算法与人生选择 分类: 转载收藏 2013...
查看>>
牛客网校招全国统一模拟笔试(三月场)- Java方向
查看>>
Apache主站点配置
查看>>
[转]蓝牙开发
查看>>
C语言程序举例
查看>>
$.param()的实例应用
查看>>
web安全:xss && csrf
查看>>
数据保存(永久保存)方式
查看>>
POJ 3320 尺取法(基础题)
查看>>
如何使表格中的文字不换行?多出的字用“..."代替
查看>>
c# 进程间通信
查看>>
Word Ladder
查看>>
ZigZag Conversion
查看>>