mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
添加53.最大子序和(动态规划)-JavaScript版
This commit is contained in:
@ -168,6 +168,23 @@ func max(a,b int) int{
|
||||
}
|
||||
```
|
||||
|
||||
JavaScript:
|
||||
|
||||
```javascript
|
||||
const maxSubArray = nums => {
|
||||
// 数组长度,dp初始化
|
||||
const [len, dp] = [nums.length, [nums[0]]];
|
||||
// 最大值初始化为dp[0]
|
||||
let max = dp[0];
|
||||
for (let i = 1; i < len; i++) {
|
||||
dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
|
||||
// 更新最大值
|
||||
max = Math.max(max, dp[i]);
|
||||
}
|
||||
return max;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user