本文后续将更新解题思路以及优化解题方法
难度中等
给定一个 没有重复 数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
class Solution {
public List<List<Integer>> permute(int[] nums) {
LinkedList<List<Integer>> list = new LinkedList<>();
permutation(list, nums, 0);
return list;
}
public void permutation(LinkedList<List<Integer>> list, int[] nums, int n) {
if (n == nums.length) {
List<Integer> l = new LinkedList<>();
for (int c : nums) {
l.add(c);
}
list.add(l);
} else {
for (int i = n; i < nums.length; i++) {
swap(nums, i, n);
permutation(list, nums, n + 1);
swap(nums, i, n);
}
}
}
public static void swap(int[] nums, int i, int n) {
int temp = nums[n];
nums[n] = nums[i];
nums[i] = temp;
}
}
难度中等
给定一个可包含重复数字的序列,返回所有不重复的全排列。
示例:
输入: [1,1,2]
输出:
[
[1,1,2],
[1,2,1],
[2,1,1]
]
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {