[LeetCode]Combination Sum III

//DFS,没有剪枝
public
class Solution { List<List<Integer>> result = new ArrayList<List<Integer>>(); public List<List<Integer>> combinationSum3(int k, int n) { helper(k, n, 0, new ArrayList<Integer>()); return result; } public void helper(int k, int n, int left, List<Integer> list) { if (k == 1) { if (left < n && n <= 9) { list.add(n); result.add(list); } return; } for (int i = left + 1; i <= 9; i++) { List<Integer> l = new ArrayList<Integer>(list); l.add(i); helper(k - 1, n - i, i, l); } } }

 

posted @ 2015-12-01 02:02  Weizheng_Love_Coding  阅读(131)  评论(0编辑  收藏  举报