mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2026-02-02 18:39:09 +08:00
Merge branch 'master' into greedy02
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
## 53. 最大子序和
|
||||
|
||||
[力扣题目链接](https://leetcode-cn.com/problems/maximum-subarray/)
|
||||
[力扣题目链接](https://leetcode.cn/problems/maximum-subarray/)
|
||||
|
||||
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
|
||||
|
||||
@@ -186,6 +186,7 @@ const maxSubArray = nums => {
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
Scala:
|
||||
|
||||
```scala
|
||||
@@ -203,5 +204,24 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function maxSubArray(nums: number[]): number {
|
||||
/**
|
||||
dp[i]:以nums[i]结尾的最大和
|
||||
*/
|
||||
const dp: number[] = []
|
||||
dp[0] = nums[0];
|
||||
let resMax: number = 0;
|
||||
for (let i = 1; i < nums.length; i++) {
|
||||
dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
|
||||
resMax = Math.max(resMax, dp[i]);
|
||||
}
|
||||
return resMax;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
<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