题目链接:https://leetcode.cn/problems/permutations/
方法一 回溯
1 方法思想
2 代码实现
class Solution {
List<List<Integer>> result = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> permute(int[] nums) {
boolean[] used = new boolean[nums.length];
backTracking(nums, used);
return result;
}
public void backTracking(int[] nums, boolean[] used){
if (path.size() == nums.length){
result.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
if (used[i]){
continue;
}
used[i] = true;
path.add(nums[i]);
backTracking(nums, used);
path.removeLast();
used[i] = false;
}
}
}
3 复杂度分析
时间复杂度:
空间复杂度: