Created
September 7, 2013 05:21
-
-
Save Jimexist/6473005 to your computer and use it in GitHub Desktop.
Generate subset with k elements
This file contains 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
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