1、汉诺塔问题。打印n层汉诺塔从最左边移动到最右边的全部过程。
public static void main(String[] args) {
method(3, "左", "右", "中");
}
public static void method(int n, String from, String to, String help) {
if (n == 1) {
System.out.println("move " + n + " from " + from + " to " + to);
return;
}
method(n - 1, from, help, to);
System.out.println("move " + n + " from " + from + " to " + to);
method(n - 1, help, to, from);
}
move 1 from 左 to 右
move 2 from 左 to 中
move 1 from 右 to 中
move 3 from 左 to 右
move 1 from 中 to 左
move 2 from 中 to 右
move 1 from 左 to 右
2、打印一个字符串的全部子序列,包括空字符串。
public static void method(String string) {
String res = "";
method(string, res, 0);
}
public static void method(String string, String res, int index) {
if (index == string.length()) {
System.out.println(res);
return;
}
//拼接当前index字符
method(string, res + string.charAt(index), index + 1);
//不拼接当前index字符
method(string, res, index + 1);
}
3、打印一个字符串的全排列。
public static void method(String string) {
char[] chars = string.toCharArray();
method(chars, 0);
}
public static void method(char[] chars, int index) {
if (index == chars.length - 1) {
System.out.println(chars);
return;
}
//依次将index位置的数值设为后面的每一个数
for (int i = index; i < chars.length; i++) {
swap(chars, index, i);
method(chars, index + 1);
swap(chars, index, i);
}
}
public static void swap(char[] chars, int i, int j) {
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
4、打印一个字符串的全部排列,要求不要出现重复的排列。
1)、加一个set去重。。
public static void method(String string) {
char[] chars = string.toCharArray();
HashSet<String> set = new HashSet<>();
method(chars, 0, set);
}
public static void method(char[] chars, int index, HashSet<String> set) {
if (index == chars.length - 1) {
if (!set.contains(String.valueOf(chars))) {
set.add(String.valueOf(chars));
System.out.println(chars);
}
return;
}
for (int i = index; i < chars.length; i++) {
if (i != index && chars[i] == chars[index]) {
continue;
}
swap(chars, index, i);
method(chars, index + 1, set);
swap(chars, index, i);
}
}
public static void swap(char[] chars, int i, int j) {
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
2)、还没想好。。
5、给你一个二维数组,二维数组中的每个数都是正数,要求从左上角走到右下角,每一步只能向右或者向下。沿途经过的数字要累加起来。返回最小的路径和。
思路:1)、从左上到右下,可以右移或下移,暴力递归枚举所有的路径,取最小的。
public static void method(int[][] arr) {
int method = method(arr, 0, 0);
System.out.println(method);
}
public static int method(int[][] arr, int i, int j) {
if (i == arr.length - 1 && j == arr[0].length - 1) {
return arr[i][j];
}
if (i == arr.length - 1) {
return arr[i][j] + method(arr, i, j + 1);
}
if (j == arr[0].length - 1) {
return arr[i][j] + method(arr, i + 1, j);
}
return Math.min(arr[i][j] + method(arr, i, j + 1), arr[i][j] + method(arr, i + 1, j));
}
2)、动态规划
从右下角开始,每个数到右下角的最短路径取决于当前数右边的数和下边的数。
public static int method2(int[][] arr, int m, int n) {
int[][] dp = new int[arr.length][arr[0].length];
int tr = arr.length - 1;
int td = arr[0].length - 1;
for (int i = tr; i >= 0; i--) {
for (int j = td; j >= 0; j--) {
if (i == tr && j == td) {
dp[i][j] = arr[i][j];
continue;
}
if (i == tr) {
dp[i][j] = dp[i][j + 1] + arr[i][j];
continue;
}
if (j == td) {
dp[i][j] = dp[i + 1][j] + arr[i][j];
continue;
}
dp[i][j] = Math.min(dp[i][j + 1], dp[i + 1][j]) + arr[i][j];
}
}
return dp[m][n];
}
6、给你一个数组arr,和一个整数aim。如果可以任意选择arr中的数字,能不能累加得到aim,返回true或者false
1、递归枚举

public static boolean method(int[] arr, int aim) {
return method(arr, aim, 0, 0);
}
public static boolean method(int[] arr, int aim, int index, int res) {
if (index == arr.length) {
if (res == aim) {
return true;
}
return false;
}
return method(arr, aim, index + 1, res + arr[index]) || method(arr, aim, index + 1, res);
}
}
6、折纸问题: 请把一段纸条竖着放在桌子上,然后从纸条的下边向上方对折1次,压出折痕后展开。此时 折痕是凹下去的,即折痕突起的方向指向纸条的背面。
如果从纸条的下边向上方连续对折2 次,压出折痕后展开,此时有三条折痕,从上到下依次是下折痕、下折痕和上折痕。
思路:折痕将字条分成两部分,下次对折时,折痕的上部会出现一个下折痕,下部会出现一个上折痕。
public static void main(String[] args) {
method(3, true);
}
public static void method(int num, boolean down) {
if (num == 0) {
return;
}
method(num - 1, true);
System.out.println(down ? "down" : "up");
method(num - 1, false);
}
down
down
up
down
down
up
up
7、给你一个栈,请你逆序这个栈,不能申请额外的数据结构,只能使用递归函数。如何实现?
思路:要逆序,就是每次都取出当前栈的最底部的数,递归返回时将其压入栈中。
//每次都取出当前栈的最底部的数,递归返回时将其压入栈中。
public static void method(Stack<Integer> stack) {
if (stack.isEmpty()) {
return;
}
int num = getAndRemoveLastNumber(stack);
method(stack);
stack.push(num);
}
//要取出最底部的数也是通过递归实现。
public static int getAndRemoveLastNumber(Stack<Integer> stack) {
int last;
if (stack.size() == 1) {
last = stack.pop();
return last;
}
int num = stack.pop();
last = getAndRemoveLastNumber(stack);
stack.push(num);
return last;
}

787

被折叠的 条评论
为什么被折叠?



