Skip to content

Instantly share code, notes, and snippets.

@Chen-tao
Created January 29, 2017 09:47
Show Gist options
  • Save Chen-tao/33ed362f55e8ffecb16e443feb776728 to your computer and use it in GitHub Desktop.
Save Chen-tao/33ed362f55e8ffecb16e443feb776728 to your computer and use it in GitHub Desktop.
//https://leetcode.com/problems/combination-sum-iii/
public class Solution {
//ans
public static List<List<Integer>> result = new ArrayList<List<Integer>>();
public static List<List<Integer>> combinationSum3(int k, int n) {
result.clear();
List<Integer> curr = new ArrayList<Integer>();
helper(curr, k, 1, n);
return result;
}
public static void helper(List<Integer> curr, int k, int start, int sum) {
if (sum < 0) {
return;
}
if (sum == 0 && curr.size() == k) {
result.add(new ArrayList<Integer>(curr));
return;
}
for (int i = start; i <= 9; i++) {
curr.add(i);
helper(curr, k, i + 1, sum - i);
curr.remove(curr.size() - 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment