Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Ensure that numbers within the set are sorted in ascending order.

Example 1:

Input: k = 3, n = 7

Output:

[[1,2,4]]

Example 2:

Input: k = 3, n = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]

思路:这道题和Combination Sum II的思路类似。只不过candidates数组里只有1-9这9个数,且增加了累加次数的限制。

 class Solution {
public:
void assist(vector<vector<int> >& res, vector<int>& cur, int target, int k, int st)
{
if (k == )
{
if (target == )
res.push_back(cur);
return;
}
for (int i = st; i <= ; i++)
{
cur.push_back(i);
assist(res, cur, target - i, k - , i + );
cur.pop_back();
}
}
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int> > res;
vector<int> cur;
assist(res, cur, n, k, );
return res;
}
};

最新文章

  1. ASP.NET Web API 配置 JSONP
  2. vpn establish capability from a remote deskstop is disabled错误的解决办法
  3. LA 3983 Robotruck
  4. NOI2003 文本编辑器
  5. js正则函数中test和match的区别
  6. Android开发之NavigationView的使用
  7. [USACO4.2]草地排水Drainage Ditches
  8. partial 的随笔
  9. 微信开发getLocation、openLocation等一些功能不起作用,但是走ready方法 closeWindow一些方法可以用
  10. 有限状态机FSM
  11. Django之CRM项目Day5-跳转页面 跟进记录 报名记录
  12. JavaSE| 面向对象-类的五大成员
  13. Spring3.X jdk8 java.lang.IllegalArgumentException
  14. 浅析 阿里 OceanBase 双十一 淘宝天猫 天量交易 承载能力 原理
  15. mybatis 返回类型为 java.lang.String 接收为null的情景
  16. loglevel-metamask
  17. 【坚持】Selenium+Python学习之从读懂代码开始 DAY7
  18. springboot基本注解
  19. centos msyql 5.7 yum安装
  20. Qt listwigwt item 加入自定义元素

热门文章

  1. CS/BS架构的特点
  2. 简述Shiro验证过程
  3. React01
  4. python中os.path.join和join的区别
  5. c++知识点总结-模板特化
  6. CyclicBarrier和CountDownLatch的使用
  7. Java性能监控之Instrumentation
  8. 个人收藏的移动端网页布局rem解决方案
  9. [bzoj3601] 一个人的数论 [莫比乌斯反演+高斯消元]
  10. POJ 3090 Visible Lattice Points | 其实是欧拉函数