Combination Sum III
来源:互联网 发布:java数组的调用 编辑:程序博客网 时间:2024/06/02 05:16
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.
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]]
方法:DFS
class Solution {private: void find(vector<vector<int>>& res, vector<int>& buffer, int index,int temp_sum, int count_number, int k,int n){ if(count_number > k || temp_sum > n){ return; } if(count_number == k && temp_sum == n){ res.push_back(buffer); return ; } for(int i = index ; i < 10; ++i){ buffer.push_back(i); find(res,buffer,i+1,temp_sum + i,count_number+1,k,n); buffer.pop_back(); } }public: vector<vector<int>> combinationSum3(int k, int n) { vector<vector<int>> res; vector<int> buffer; find(res,buffer,1,0,0,k,n); return res; }};
0 0
- Combination Sum II&III
- Combination Sum III
- LeetCode Combination Sum III
- Combination Sum III
- [LeetCode] Combination Sum III
- Combination Sum III
- 【leetcode】 Combination Sum III
- [leetcode] Combination Sum III
- [leetCode] Combination Sum III
- leetcode Combination Sum III
- leetcode--Combination Sum III
- LeetCode216:Combination Sum III
- Combination Sum III
- LeetCode Combination Sum III
- Combination Sum III
- Combination Sum III
- Leetcode: Combination Sum III
- leetcode: Combination Sum III
- 剑指offer 38. 数字在排序数组中出现的次数
- 数论常用内容——整除
- UC伯克利、谷歌无监督深度学习框架,模仿人眼实现视频中的自我运动认知
- 获取多个rowguid字符串
- 数组指针VS指针数组,函数指针VS指针函数
- Combination Sum III
- 斯坦福机器学习笔记七
- SuperMap iDesktop 8C 地图制图
- 深入理解JS—对象和原型
- 谷歌推出有界负载的一致性哈希算法,解决服务器负载均衡问题
- 基于《Selenium 2自动化测试实战》的学习笔记(9)—— 键盘事件
- 利用pandas实现从数据库读写数据
- 读书进阶
- app widget简单用法(1)