class Solution {
public int maxSubArray(int[] nums) {
int pre = 0, maxAns = nums[0];
for (int x : nums) {
pre = Math.max(pre + x, x);
maxAns = Math.max(maxAns, pre);
}
return maxAns;
}
}
2022-01-18(剑指 Offer 42. 连续子数组的最大和)
最新推荐文章于 2025-12-06 18:30:00 发布
本文介绍了一种求解一维数组中最大子序和的高效算法实现。通过使用预处理变量跟踪当前子序列的最大和,并更新全局最大答案,实现了O(n)的时间复杂度。
296

被折叠的 条评论
为什么被折叠?



