Created
January 29, 2017 09:47
-
-
Save Chen-tao/33ed362f55e8ffecb16e443feb776728 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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