LeetCode -- Combination Sum III
2015-10-30 23:19
381 查看
题目描述:
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]]
就是从1到9构成的数组{1,2,3,4,5,6,7,8,9}中,找到一种组合,包含K个元素,和为n。
思路:
是一个典型的回溯问题,也算是问题:从数组nums中找到所有和为n的组合情况的子问题。
运用回溯模型直接解即可。 在遍历过程中,只需要收集只有k个元素的组合。
实现代码:
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]]
就是从1到9构成的数组{1,2,3,4,5,6,7,8,9}中,找到一种组合,包含K个元素,和为n。
思路:
是一个典型的回溯问题,也算是问题:从数组nums中找到所有和为n的组合情况的子问题。
运用回溯模型直接解即可。 在遍历过程中,只需要收集只有k个元素的组合。
实现代码:
public class Solution { public IList<IList<int>> CombinationSum3(int k, int n) { var result = new List<IList<int>>(); Find(1, n, k, new List<int>(), ref result); return result; } private void Find(int start, int target, int k, IList<int> current, ref List<IList<int>> result) { if(current.Count > k || target < 0) { return; } if(target == 0) { if(current.Count == k){ result.Add(new List<int>(current)); } //Console.WriteLine(current); return; } for(var i = start; i < 10; i++) { current.Add(i); Find(i + 1, target - i, k, current,ref result); current.RemoveAt(current.Count-1); } } }
相关文章推荐
- 触摸事件分发机制详解
- 关于VFS文件系统中的superblock、inode、d_entry和file数据结构
- ANDROID开发环境
- hdu 4643 GSM(几何)
- 结构体和指针
- Android
- Android
- 冲不动刺。。
- LeetCode -- Binary Search Tree Iterator
- shell find和xargs学习(七)
- inet_ntoa函数在编译时候报 ”warning: assignment makes pointer from integer without a cast“ 告警
- 1044. 火星数字(20)
- 从“Android群英传”谈开去
- 贪心--HDU - 4442 Physical Examination
- 反射机制----Constructor,数组的反射机制
- UI中自定制cell--3
- 字符串倒序
- hdu 5073 Galaxy 2014 鞍山区域赛 数学
- [细节]Tomcat对静态资源的缓存支持
- Win环境下本地运行hadoop1版本出现访问权限问题 Failed to set permissions of path