zoukankan      html  css  js  c++  java
  • [leetcode]Unique Binary Search Trees

    动态规划。不过一开始老是把循环中的i写成n。有的时候会忘记i是循环中的边界。

    public class Solution {
        public int numTrees(int n) {
            if (n ==0) return 0;
            int sum[] = new int[n+1];
            sum[1] = 1;
            for (int i = 2; i <= n; i++) {
                int total = 0;
                for (int j = 1; j <=i; j++) {
                    if (j == 1 || j == i) total += sum[i-1];
                    else {
                        total+= (sum[j-1]*sum[i-j]);
                    }
                }
                sum[i] = total; 
            }
            return sum[n];
        }
    }
    

      

  • 相关阅读:
    RabbitMq
    SAAS-HEM
    java框架
    数据结构
    JVM
    springboot高级
    面试题汇总
    vue
    bootforum
    SpringBoot
  • 原文地址:https://www.cnblogs.com/lautsie/p/3267045.html
Copyright © 2011-2022 走看看