leet-code/src/main/java/leetcode/editor/cn/LianXuZiShuZuDeZuiDaHeLcof.java
2021-07-17 22:39:01 +08:00

57 lines
1.5 KiB
Java
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//输入一个整型数组,数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。
//
// 要求时间复杂度为O(n)。
//
//
//
// 示例1:
//
// 输入: nums = [-2,1,-3,4,-1,2,1,-5,4]
//输出: 6
//解释: 连续子数组 [4,-1,2,1] 的和最大 6。
//
//
//
// 提示:
//
//
// 1 <= arr.length <= 10^5
// -100 <= arr[i] <= 100
//
//
// 注意:本题与主站 53 题相同https://leetcode-cn.com/problems/maximum-subarray/
//
//
// Related Topics 数组 分治 动态规划
// 👍 339 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//剑指 Offer 42:连续子数组的最大和
class LianXuZiShuZuDeZuiDaHeLcof {
public static void main(String[] args) {
//测试代码
Solution solution = new LianXuZiShuZuDeZuiDaHeLcof().new Solution();
System.out.println(solution.maxSubArray(new int[]{-1}));
System.out.println(solution.maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}));
System.out.println(solution.maxSubArray(new int[]{-2, -1}));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int maxSubArray(int[] nums) {
int cur = 0;
int max = Integer.MIN_VALUE;
for (int num : nums) {
cur = Math.max(cur + num, num);
max = Math.max(max, cur);
}
return max;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}