simple formula


  • 1
    S

    Just use a simple formula: (n + 1) * (n + 2) * ··· * 2n / (n + 1)!

    public class Solution {
      public int numTrees(int n) {
          long result = 1;
          for(int i = n + 1; i <= 2 * n; i ++) {
              result *= i;
              result /= (i - n);
          }
          result /= n + 1;
          return (int)result;
      }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.