diff --git a/problems/0063.不同路径II.md b/problems/0063.不同路径II.md index 59c60156..88fce505 100644 --- a/problems/0063.不同路径II.md +++ b/problems/0063.不同路径II.md @@ -440,5 +440,37 @@ int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridSize, int* obst } ``` +### Scala + +```scala +object Solution { + import scala.util.control.Breaks._ + def uniquePathsWithObstacles(obstacleGrid: Array[Array[Int]]): Int = { + var (m, n) = (obstacleGrid.length, obstacleGrid(0).length) + var dp = Array.ofDim[Int](m, n) + + // 比如break、continue这些流程控制需要使用breakable + breakable( + for (i <- 0 until m) { + if (obstacleGrid(i)(0) != 1) dp(i)(0) = 1 + else break() + } + ) + breakable( + for (j <- 0 until n) { + if (obstacleGrid(0)(j) != 1) dp(0)(j) = 1 + else break() + } + ) + + for (i <- 1 until m; j <- 1 until n; if obstacleGrid(i)(j) != 1) { + dp(i)(j) = dp(i - 1)(j) + dp(i)(j - 1) + } + + dp(m - 1)(n - 1) + } +} +``` + -----------------------