Skip to content

Instantly share code, notes, and snippets.

@mmaxiaolei
Created March 22, 2018 09:14
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 mmaxiaolei/6bc4f3fe9432086eab1b690d3dde9d29 to your computer and use it in GitHub Desktop.
Save mmaxiaolei/6bc4f3fe9432086eab1b690d3dde9d29 to your computer and use it in GitHub Desktop.
public List<List<Integer>> solution(List<Integer> n, int m) {
List<List<Integer>> result = new ArrayList<>();
helper(m, result, new ArrayList<>(), n);
return result;
}
private void helper(int size, List<List<Integer>> result, List<Integer> current, List<Integer> other) {
// 剪枝
if (current.size() == size) {
result.add(new ArrayList<>(current));
return;
}
for (Integer integer : other) {
current.add(integer);
List<Integer> newOther = new ArrayList<>(other);
newOther.remove(integer);
helper(size, result, current, newOther);
// 回溯到上一层
current.remove(current.size() - 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment