- https://leetcode-cn.com/problems/permutations/
https://leetcode-cn.com/problems/permutations-ii/
问题描述
问题思路
代码思路
-
代码实现
```java package com.wztlink1013.al.递归法; import java.util.ArrayList;
/**
- 全排列问题
- */
public class Permuting {
static ArrayList
public static void main(String args[]) {
int n = 10;
Times.test("当n = " + n + "时候的耗费时间", new Times.Task() {
public void execute() {
PermutingCaller(n);
}
});
}
private static void PermutingCaller(int n) {
N = n;
for (int i = 0; i < n; ++i)
x.add(i + 1);
Permuting(0);
}
private static void Permuting(int i) {
if (i < N - 1) {
for (int j = i; j < N; j++) {
t = x.get(i);
x.set(i, x.get(j));
x.set(j, t);
Permuting(i + 1);
t = x.get(i);
x.set(i, x.get(j));
x.set(j, t);
}
}
else
OutputOnePermutation();
}
private static void OutputOnePermutation()
{
System.out.print(++cnt + " : ");
for (int x : x)
System.out.print(x + " ");
System.out.println();
}
} ```
n:10
n