一些经典的递归

86 阅读1分钟

汉诺塔

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);
   }
}

打印所有子序列

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> 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;
}

// 在现有的字母中,在前面收集了的path下,收集ans
public static void f(ArrayList<Character> rest, String path, List<String> ans) {
   if (rest.isEmpty()) {  // 没有字母了,收集ans
      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); // 要恢复现场
      }
   }
}

方法二

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;
}

// index往后所有字母,求全排列收集到ans里去
public static void g1(char[] str, int index, List<String> ans) {
   if (index == str.length) { // 指到头了,收集ans
      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 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;
   }
}

public static void main(String[] args) {
   Stack<Integer> test = new Stack<Integer>();
   test.push(1);
   test.push(2);
   test.push(3);
   test.push(4);
   test.push(5);
   reverse(test);
   while (!test.isEmpty()) {
      System.out.println(test.pop());
   }

}