forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumSubarray.java
More file actions
24 lines (22 loc) · 543 Bytes
/
MaximumSubarray.java
File metadata and controls
24 lines (22 loc) · 543 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
package LeetCode;
import java.util.Arrays;
/**
* @Classname MaximumSubarray
* @Description TODO
* @Date 19-2-28 上午10:24
* @Created by mao<tianmao818@qq.com>
*/
public class MaximumSubarray {
public int maxSubArray(int[] nums) {
int len=nums.length;
int sum[]=new int[len];
sum[0]=nums[0];
for(int i=1;i<len;i++){
sum[i]=max(nums[i],nums[i]+sum[i-1]);
}
return Arrays.stream(sum).max().getAsInt();
}
public int max(int a,int b){
return a>b? a:b;
}
}