Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b1ef364

Browse files
添加(0096.不同的二叉搜索树.md):增加typescript版本
1 parent 6877683 commit b1ef364

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

‎problems/0096.不同的二叉搜索树.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,33 @@ const numTrees =(n) => {
227227
};
228228
```
229229

230-
C:
230+
TypeScript
231+
232+
```typescript
233+
function numTrees(n: number): number {
234+
/**
235+
dp[i]: i个节点对应的种树
236+
dp[0]: -1; 无意义;
237+
dp[1]: 1;
238+
...
239+
dp[i]: 2 * dp[i - 1] +
240+
(dp[1] * dp[i - 2] + dp[2] * dp[i - 3] + ... + dp[i - 2] * dp[1]); 从1加到i-2
241+
*/
242+
const dp: number[] = [];
243+
dp[0] = -1; // 表示无意义
244+
dp[1] = 1;
245+
for (let i = 2; i <= n; i++) {
246+
dp[i] = 2 * dp[i - 1];
247+
for (let j = 1, end = i - 1; j < end; j++) {
248+
dp[i] += dp[j] * dp[end - j];
249+
}
250+
}
251+
return dp[n];
252+
};
253+
```
254+
255+
### C
256+
231257
```c
232258
//开辟dp数组
233259
int *initDP(int n) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /