Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Return a list of all the subsequences of `li' with length `n'.
- // http://cadrlife.blogspot.com/2008/03/returning-considered-difficult.html
- private static <T> List<List<T>> subn(int n, List<T> li) {
- if (n == 0) {
- return Collections.<List<T>>singletonList(new ArrayList<T>());
- }
- if (li.isEmpty()) {
- return new ArrayList<List<T>>();
- }
- List<List<T>> ret = new ArrayList<List<T>>();
- T x = li.get(0);
- List<T> xs = li.subList(1, li.size());
- for (List<T> sub : subn(n-1, xs)) {
- sub.add(0, x);
- ret.add(sub);
- }
- ret.addAll(subn(n, xs));
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement