打印n层汉诺塔从最左边移动到最右边的全部过程
在 func 方法中,当盘子数量 N 为 1 时,这是最基本的情况,直接输出将 1 号盘子从起始柱 from 移动到目标柱 to 。
当 N 大于 1 时,它分为三步来处理。第一步,先将 N-1 个盘子通过目标柱 to 作为辅助柱,从起始柱 from 移动到中间柱 other ;第二步,将剩下的 1 个盘子(也就是最大的那个盘子)从起始柱 from 移动到目标柱 to ;第三步,再将中间柱 other 上的 N-1 个盘子通过起始柱 from 作为辅助柱,移动到目标柱 to
public static void hanoi2(int n) {
if (n > 0) {
func(n, "left", "right", "mid");
}
}
public static void func(int N, String from, String to, String other) {
if (N == 1) { // base
System.out.println("Move 1 from " + from + " to " + to);
} else {
func(N - 1, from, other, to);
System.out.println("Move " + N + " from " + from + " to " + to);
func(N - 1, other, to, from);
}
}
打印一个字符串的全部子序列
// s -> "abc" ->
public static List<String> subs(String s) {
char[] str = s.toCharArray();
String path = "";
List<String> ans = new ArrayList<>();
process1(str, 0, ans, path);
return ans;
}
// str 固定参数
// 来到了str[index]字符,index是位置
// str[0..index-1]已经走过了!之前的决定,都在path上
// 之前的决定已经不能改变了,就是path
// str[index....]还能决定,之前已经确定,而后面还能自由选择的话,
// 把所有生成的子序列,放入到ans里去
public static void process1(char[] str, int index, List<String> ans, String path) {
if (index == str.length) {
ans.add(path);
return;
}
// 没有要index位置的字符
process1(str, index + 1, ans, path);
// 要了index位置的字符
process1(str, index + 1, ans, path + String.valueOf(str[index]));
}
打印一个字符串的全部子序列,要求不要出现重复字面值的子序列
public static List<String> subsNoRepeat(String s) {
char[] str = s.toCharArray();
String path = "";
HashSet<String> set = new HashSet<>();
process2(str, 0, set, path);
List<String> ans = new ArrayList<>();
for (String cur : set) {
ans.add(cur);
}
return ans;
}
public static void process2(char[] str, int index, HashSet<String> set, String path) {
if (index == str.length) {
set.add(path);
return;
}
String no = path;
process2(str, index + 1, set, no);
String yes = path + String.valueOf(str[index]);
process2(str, index + 1, set, yes);
}
打印一个字符串的全部排列
法1:
首先进入 permutation1("abc") 方法。
将 "abc" 转换为字符数组 ['a', 'b', 'c'] ,并将字符添加到 rest 列表中。
然后调用 f(rest, "", ans) 。
在第一次进入 f 方法时, rest 为 ['a', 'b', 'c'] , path 为空。
进入循环,第一次取到 'a' ,移除 'a' 后, rest 变为 ['b', 'c'] ,然后递归调用 f(['b', 'c'], "a", ans) 。
在这个递归调用中,又会进行类似的操作,比如取到 'b' ,移除 'b' 后, rest 变为 ['c'] ,继续递归调用 f(['c'], "ab", ans) 。此时 rest 为空,将 "ab" 添加到 ans 中。然后回溯,将 'b' 加回 rest 。
接着再取 'c' 进行类似操作,得到 "ac" 添加到 ans 中。
当这一轮循环结束后,回溯到最初的调用,将 'a' 加回 rest 。
然后继续循环,取到 'b' 进行类似过程,会得到 "ba" ,"bc" 等排列
最后取到 'c' ,得到 "ca" ,"cb" 等排列。
最终 ans 中就包含了 "abc" ,"acb" ,"bac" ,"bca" ,"cab" ,"cba"
public static List<String> permutation1(String s) {
List<String> ans = new ArrayList<>();
if (s == null || s.length() == 0) {
return ans;
}
char[] str = s.toCharArray();
ArrayList<Character> rest = new ArrayList<Character>();
for (char cha : str) {
rest.add(cha);
}
String path = "";
f(rest, path, ans);
return ans;
}
public static void f(ArrayList<Character> rest, String path, List<String> ans) {
if (rest.isEmpty()) {
ans.add(path);
} else {
int N = rest.size();
for (int i = 0; i < N; i++) {
char cur = rest.get(i);
rest.remove(i);
f(rest, path + cur, ans);
rest.add(i, cur);
}
}
}
法2:
public static List<String> permutation2(String s) {
List<String> ans = new ArrayList<>();
if (s == null || s.length() == 0) {
return ans;
}
char[] str = s.toCharArray();
g1(str, 0, ans);
return ans;
}
public static void g1(char[] str, int index, List<String> ans) {
if (index == str.length) {
ans.add(String.valueOf(str));
} else {
for (int i = index; i < str.length; i++) {
swap(str, index, i);
g1(str, index + 1, ans);
swap(str, index, i);
}
}
}
打印一个字符串的全部排列,要求不要出现重复的排列
public static List<String> permutation3(String s) {
List<String> ans = new ArrayList<>();
if (s == null || s.length() == 0) {
return ans;
}
char[] str = s.toCharArray();
g2(str, 0, ans);
return ans;
}
public static void g2(char[] str, int index, List<String> ans) {
if (index == str.length) {
ans.add(String.valueOf(str));
} else {
boolean[] visited = new boolean[256];
for (int i = index; i < str.length; i++) {
if (!visited[str[i]]) {
visited[str[i]] = true;
swap(str, index, i);
g2(str, index + 1, ans);
swap(str, index, i);
}
}
}
}
public static void swap(char[] chs, int i, int j) {
char tmp = chs[i];
chs[i] = chs[j];
chs[j] = tmp;
}
给你一个栈,请你逆序这个栈,不能申请额外的数据结构,只能使用递归函数。如何实现?
public static void reverse(Stack<Integer> stack) {
if (stack.isEmpty()) {
return;
}
int i = f(stack);
reverse(stack);
stack.push(i);
}
// 栈底元素移除掉
// 上面的元素盖下来
// 返回移除掉的栈底元素
public static int f(Stack<Integer> stack) {
int result = stack.pop();
if (stack.isEmpty()) {
return result;
} else {
int last = f(stack);
stack.push(result);
return last;
}
}