mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #1276 from xiaofei-2020/dp09
添加(0096.不同的二叉搜索树.md):增加typescript版本
This commit is contained in:
@ -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) {
|
||||
|
Reference in New Issue
Block a user