-
-
Save mmaxiaolei/6bc4f3fe9432086eab1b690d3dde9d29 to your computer and use it in GitHub Desktop.
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 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