forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxSubArray.java
More file actions
37 lines (33 loc) · 955 Bytes
/
MaxSubArray.java
File metadata and controls
37 lines (33 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package normal;
/**
* @program JavaBooks
* @description: 53. 最大子序和
* @author: mf
* @create: 2019/11/10 10:37
*/
/*
题目:https://leetcode-cn.com/problems/maximum-subarray/
类型:动态规划
难度:easy
*/
/*
输入: [-2,1,-3,4,-1,2,1,-5,4],
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
*/
public class MaxSubArray {
public static void main(String[] args) {
int[] nums = {-2,1,-3,4,-1,2,1,-5,4};
System.out.println(maxSubArray(nums));
}
public static int maxSubArray(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int max = nums[0]; // 记录包含arr[i]的连续子数组的和的最大值
int ans = nums[0]; // 记录当前所有子数组的和的最大值
for (int i = 1; i < nums.length; i++) {
max = Math.max(max + nums[i], nums[i]);
ans = Math.max(max, ans);
}
return ans;
}
}