-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0041_最大子数组.java
More file actions
32 lines (32 loc) · 772 Bytes
/
Copy path0041_最大子数组.java
File metadata and controls
32 lines (32 loc) · 772 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
public class Solution {
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
public int maxSubArray(int[] nums) {
// write your code
if(nums==null||nums.length==0){
return 0;
}
if(nums.length==1){
return nums[0];
}
int len=nums.length;
int[] dp=new int[len];
dp[0]=nums[0];
for(int i=1;i<len;i++){
if(dp[i-1]>0){
dp[i]=nums[i]+dp[i-1];
}else{
dp[i]=nums[i];
}
}
int max=Integer.MIN_VALUE;
for(int i=0;i<len;i++){
if(dp[i]>max){
max=dp[i];
}
}
return max;
}
}