-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxSubArray.java
More file actions
executable file
·29 lines (26 loc) · 665 Bytes
/
Copy pathmaxSubArray.java
File metadata and controls
executable file
·29 lines (26 loc) · 665 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
package dynamic;
/**
* Created by Administrator on 2018/3/31 0031.
*/
public class maxSubArray {
public int maxSubArray(int[] nums) {
int length=nums.length;
int sum=nums[0];
int tmpnum=0;
for(int i=0;i<length;i++){
tmpnum+=nums[i];
if(tmpnum>sum){
sum=tmpnum;
}
if(tmpnum<=0) {
tmpnum = 0;
}
}
return sum;
}
public static void main(String[] args) {
int[] a={-3,-2,0,-1};
maxSubArray max=new maxSubArray();
System.out.println(max.maxSubArray(a));
}
}