LeetCode 46. Permutations
原题链接
简单
作者:
HK
,
2024-03-04 20:30:07
,
所有人可见
,
阅读 26
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Boolean> status = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
if(nums.length == 0){
return res;
}
for (int i = 0; i < nums.length; i++) {
status.add(false);
}
dfs(nums, 0);
return res;
}
public void dfs(int[] nums, int u){
if(u == nums.length){
res.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
if(status.get(i) == false){
status.set(i, true);
path.add(nums[i]);
dfs(nums, u+1);
status.set(i, false);
path.remove(path.size() - 1);
}
}
}
}