class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
dfs(nums, 0, nums.length, res);
return res;
}
public void dfs(int[] nums, int k, int n, List<List<Integer>> res){
if (k == n){
List<Integer> item = new ArrayList<Integer>();
for (int i = 0; i < n; i++){
item.add(nums[i]);
}
res.add(item);
} else {
for (int i = k; i < n; i++){
swap(nums, i, k);
dfs(nums, k + 1, n, res);
swap(nums, i, k);
}
}
}
public void swap(int[] nums, int i, int j){
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
LeetCode 刷题 ---47全排列
最新推荐文章于 2023-03-06 21:45:28 发布