首页 > 其他 > 详细

216. Combination Sum III

时间:2016-07-24 08:11:13      阅读:145      评论:0      收藏:0      [点我收藏+]

backtracking,自己很快写出来了,很开心呢

 1     public List<List<Integer>> combinationSum3(int k, int n) {
 2         List<List<Integer>> res = new ArrayList<List<Integer>>();
 3         helper(res, new ArrayList<Integer>(), 1, k, n, 0);
 4         return res;
 5     }
 6     
 7     private void helper(List<List<Integer>> res, List<Integer> item, int start, int k, int n, int curSum) {
 8         if(item.size() == k) {
 9             if(curSum == n) {
10                 res.add(new ArrayList<Integer>(item));
11             }
12             return;
13         }
14         for(int i = start; i <= 9; i++) {
15             if(curSum + i <= n) {
16                 item.add(i);
17                 helper(res, item, i + 1, k, n, curSum + i);
18                 item.remove(item.size() - 1);
19             }
20         }
21     }

 

216. Combination Sum III

原文:http://www.cnblogs.com/warmland/p/5700030.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!