添加(0096.不同的二叉搜索树.md):增加typescript版本

This commit is contained in:
Steve2020
2022-05-02 17:25:45 +08:00
parent 6877683c95
commit b1ef364ffb

View File

@ -227,7 +227,33 @@ const numTrees =(n) => {
};
```
C:
TypeScript
```typescript
function numTrees(n: number): number {
/**
dp[i]: i个节点对应的种树
dp[0]: -1; 无意义;
dp[1]: 1;
...
dp[i]: 2 * dp[i - 1] +
(dp[1] * dp[i - 2] + dp[2] * dp[i - 3] + ... + dp[i - 2] * dp[1]); 从1加到i-2
*/
const dp: number[] = [];
dp[0] = -1; // 表示无意义
dp[1] = 1;
for (let i = 2; i <= n; i++) {
dp[i] = 2 * dp[i - 1];
for (let j = 1, end = i - 1; j < end; j++) {
dp[i] += dp[j] * dp[end - j];
}
}
return dp[n];
};
```
### C
```c
//开辟dp数组
int *initDP(int n) {