zoukankan      html  css  js  c++  java
  • 96. Unique Binary Search Trees

    not difficult, it is easy

    first you should decide the root num, then the litter num is left of it, the bigger nums are right of it;
    we can get that
    f(n) = f(n-1) + f(1) * f(n-2) + f(2) * f(n-3) +... + f(n-2) * f(1) + f(n-1)
    we can makesure that f(0) = 1
    and then
    f(n) = ∑f(k) * f(n-1-k)
    so we use db solution

    class Solution {
    public:
        vector<int> kinds_num(int n){
            vector<int> result;
            result.push_back(1);
            for (int i=1;i<=n;i++){
                int tmp = 0;
                for (int j=0;j<=i-1;j++){
                    tmp += result[j] * result[i-1-j];
                }
                result.push_back(tmp);
            }
            return result;
        }
        int numTrees(int n) {
            vector<int> result;
            result = kinds_num(n);
            return result[n];
        }
    };
  • 相关阅读:
    day4笔记
    day3笔记
    day2 python基础 while 循环补充
    day1笔记 初识python,paython基础
    指针-1
    scanf输入与getchar的比较
    [递归]母牛的故事
    安装mysql以及遇到的问题解决
    33.指针
    32.字符串
  • 原文地址:https://www.cnblogs.com/mangmangbiluo/p/11033209.html
Copyright © 2011-2022 走看看