mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 0096.不同的二叉搜索树.md Scala版本
This commit is contained in:
@ -227,7 +227,7 @@ const numTrees =(n) => {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript
|
### TypeScript
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function numTrees(n: number): number {
|
function numTrees(n: number): number {
|
||||||
@ -282,5 +282,22 @@ int numTrees(int n){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Scala
|
||||||
|
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
def numTrees(n: Int): Int = {
|
||||||
|
var dp = new Array[Int](n + 1)
|
||||||
|
dp(0) = 1
|
||||||
|
for (i <- 1 to n) {
|
||||||
|
for (j <- 1 to i) {
|
||||||
|
dp(i) += dp(j - 1) * dp(i - j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dp(n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
Reference in New Issue
Block a user