mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #1486 from wzqwtt/dp03
添加(0343.整数拆分、0096.不同的二叉搜索树) Scala版本
This commit is contained in:
@ -227,7 +227,7 @@ const numTrees =(n) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript
|
||||
### TypeScript
|
||||
|
||||
```typescript
|
||||
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>
|
||||
|
@ -335,5 +335,22 @@ int integerBreak(int n){
|
||||
}
|
||||
```
|
||||
|
||||
### Scala
|
||||
|
||||
```scala
|
||||
object Solution {
|
||||
def integerBreak(n: Int): Int = {
|
||||
var dp = new Array[Int](n + 1)
|
||||
dp(2) = 1
|
||||
for (i <- 3 to n) {
|
||||
for (j <- 1 until i - 1) {
|
||||
dp(i) = math.max(dp(i), math.max(j * (i - j), j * dp(i - j)))
|
||||
}
|
||||
}
|
||||
dp(n)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<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