最简单的深度搜索,空间复杂度也比较高,花费在List的复制上。
public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<Integer> left = new ArrayList<Integer>();
for (int num : nums) {
left.add(num);
}
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> in = new ArrayList<Integer>();
dfs(in, left, left.size(), result);
return result;
}
// in为已经进入排列的,left为nums除掉in剩下的
public void dfs(List<Integer> in, List<Integer> left, int length, List<List<Integer>> result) {
if (in.size() == length) {
result.add(in);
return;
}
for (int i = 0; i < left.size(); i++) {
List<Integer> newIn = new ArrayList<Integer>(in);
List<Integer> newLeft = new ArrayList<Integer>(left);
newIn.add(left.get(i));
newLeft.remove(i);
dfs(newIn, newLeft, length, result);
}
}
}
基于交换与递归的全排列
public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<Integer> left = new ArrayList<Integer>();
for (int num : nums) {
left.add(num);
}
List<List<Integer>> results = new ArrayList<List<Integer>>();
List<Integer> in = new ArrayList<Integer>();
dfs(nums, 0, results);
return results;
}
public void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
public void dfs(int[] nums, int l, List<List<Integer>> results) {
if (l == nums.length) {
List<Integer> result = new ArrayList<Integer>();
for (int num : nums) {
result.add(num);
}
results.add(result);
}
for (int i = l; i < nums.length; i++) {
swap(nums, l, i);
dfs(nums, l+1, results);
swap(nums, l, i);
}
}
}
基于迭代的全排列
字典序法,参考字典序全排列算法研究
【例】 如何得到346987521的下一个
1,从尾部往前找第一个P(i-1) < P(i)的位置3 4 6 <- 9 <- 8 <- 7 <- 5 <- 2 <- 1 最终找到6是第一个变小的数字,记录下6的位置i-1 2,从i位置往后找到最后一个大于6的数 3 4 6 -> 9 -> 8 -> 7 5 2 1 最终找到7的位置,记录位置为m 3,交换位置i-1和m的值 3 4 7 9 8 6 5 2 1 4,倒序i位置后的所有数据 3 4 7 1 2 5 6 8 9 则347125689为346987521的下一个排列
public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> results = new ArrayList<List<Integer>>();
List<Integer> source = new ArrayList<Integer>();
for (int num : nums) {
source.add(num);
}
Collections.sort(source);
for (int i = 0; i < source.size(); i++) {
nums[i] = source.get(i);
}
while(next_permute(nums, results)) {
}
return results;
}
public void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
public boolean next_permute(int[] nums, List<List<Integer>> results) {
List<Integer> result = new ArrayList<Integer>();
for (int num : nums) {
result.add(num);
}
results.add(result);
// 1. from right to left find the partition number, ther first voilate increasing number
int length = nums.length;
int partitionKey = -1;
for (int i = length - 1; i > 0; i-- ) {
if (nums[i] > nums[i - 1]) {
partitionKey = i - 1;
break;
}
}
if (partitionKey == -1) {
return false;
}
// 2. from right to left find the first change number larger than the partition number
int changeKey = -1;
for (int i = length - 1; i > 0; i-- ) {
if (nums[i] > nums[partitionKey]) {
changeKey = i;
break;
}
}
swap(nums, partitionKey, changeKey);
// 3. reverse all the nums at the right of partitionKey
for (int i = partitionKey + 1; i <= (partitionKey+length)/2 ;i++) {
swap(nums, i, partitionKey + length - i);
}
return true;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。