Skip to content

Instantly share code, notes, and snippets.

@Jimexist
Created September 7, 2013 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jimexist/6473005 to your computer and use it in GitHub Desktop.
Save Jimexist/6473005 to your computer and use it in GitHub Desktop.
Generate subset with k elements
public class Solution {
public ArrayList<ArrayList<Integer>> combine(int n, int k) {
final ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if (k == 0) {
result.add(new ArrayList<Integer>());
return result;
}
for (int i=n; i>=1; --i) {
for (ArrayList<Integer> ai : combine(i-1, k-1)) {
ai.add(i);
result.add(ai);
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment