diff --git a/problems/0096.不同的二叉搜索树.md b/problems/0096.不同的二叉搜索树.md index 9d98c62d..9adc0457 100644 --- a/problems/0096.不同的二叉搜索树.md +++ b/problems/0096.不同的二叉搜索树.md @@ -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) + } +} +``` + -----------------------
diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index dd03937f..a4d532fd 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -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) + } +} +``` + -----------------------