mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
添加 0053.最大子序和(动态规划).md Scala版本
This commit is contained in:
@ -186,7 +186,22 @@ const maxSubArray = nums => {
|
||||
};
|
||||
```
|
||||
|
||||
Scala:
|
||||
|
||||
```scala
|
||||
object Solution {
|
||||
def maxSubArray(nums: Array[Int]): Int = {
|
||||
var dp = new Array[Int](nums.length)
|
||||
var result = nums(0)
|
||||
dp(0) = nums(0)
|
||||
for (i <- 1 until nums.length) {
|
||||
dp(i) = math.max(nums(i), dp(i - 1) + nums(i))
|
||||
result = math.max(result, dp(i)) // 更新最大值
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<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