LeetCode 46. 全排列
原题链接
中等
作者:
LangB
,
2020-10-30 20:25:38
,
所有人可见
,
阅读 257
class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
boolean[] used = new boolean[nums.length];
dfs(nums, new ArrayList<Integer>(), used);
return res;
}
private void dfs(int[] nums, List<Integer> path, boolean[] used) {
if (path.size() == nums.length) {
res.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
if (!used[i]) {
used[i] = true;
path.add(nums[i]);
dfs(nums, path, used);
path.remove(path.size() - 1);
used[i] = false;
}
}
}
}