标签:
public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> res = new ArrayList<List<Integer>>(); List<Integer> item = new ArrayList<Integer>(); int sum[] = {0}; dfs(k, n, res, item, 1, sum); return res; } public void dfs(int k, int n, List<List<Integer>> res, List<Integer> item, int depth, int[] sum) { if (item.size() > k || sum[0] > n) return; if (item.size() == k && sum[0] == n) { res.add(new ArrayList<Integer>(item)); return; } for (int i = depth; i <=9; i++) { item.add(i); sum[0] += i; dfs(k, n, res, item, i+1, sum); item.remove(item.size() - 1); sum[0] -= i; } }
标签:
原文地址:http://www.cnblogs.com/77rousongpai/p/4539783.html