看雪Unicorn高級逆向與反混淆
不同的二叉搜索樹
public class BinarySearchTree {
? ?// f(n) n個節(jié)點二叉搜索樹
? ?// g(i) i為根節(jié)點,n個節(jié)點的二叉搜索樹
? ?// ? ? ?左子樹一定有 i - 1 個元素 => 左子樹有 f(i-1) 種情況
? ?// ? ? ?右子樹一定有 n - i 個元素 => 右子樹有 f(n-i) 中情況
? ?// f(n) = g(1) + g(2) + g(3) + ... + g(n-1) + g(n)
? ?// ? ? ?= f(0) * f(n-1) + f(1) * f(n-2) + f(2) * f(n-3) + ... + f(n-2) * f(1) + f(n-1) * f(0)
? ?// f(3) = g(1) + g(2) + g(3)
? ?// g(1) 左子樹一定有0個元素,右子樹一定有2個元素
? ?// g(2) 左子樹一定有1個元素,右子樹一定有1個元素
? ?// g(3) 左子樹一定有2個元素,右子樹一定有0個元素
? ?public static int recursion(int n) {
? ? ? ?if (n == 0) {
? ? ? ? ? ?return 1;
? ? ? ?}
? ? ? ?if (n == 1 || n == 2) {
? ? ? ? ? ?return n;
? ? ? ?}
? ? ? ?int result = 0;
? ? ? ?for (int i = 0; i < n; i++) {
? ? ? ? ? ?result += recursion(i) * recursion(n - i - 1);
? ? ? ?}
? ? ? ?return result;
? ?}
? ?public static int loop(int n) {
? ? ? ?int[] result = new int[n + 1];
? ? ? ?result[0] = 1;
? ? ? ?if (n >= 0) {
? ? ? ? ? ?result[1] = 1;
? ? ? ?}
? ? ? ?if (n >= 1) {
? ? ? ? ? ?result[2] = 2;
? ? ? ?}
? ? ? ?for (int i = 3; i <= n; i++) {
? ? ? ? ? ?// 計算第i個位置上的結(jié)果,第i個位置上的結(jié)果由前i-1個結(jié)果生成
? ? ? ? ? ?int sum = 0;
? ? ? ? ? ?for (int j = 0; j < i; j++) {
? ? ? ? ? ? ? ?sum += result[j] * result[i - j - 1];
? ? ? ? ? ?}
? ? ? ? ? ?result[i] = sum;
? ? ? ?}
? ? ? ?return result[n];
? ?}
? ?public static void main(String[] args) {
? ? ? ?System.out.println(loop(3));
? ?}}