forked from Wang-Jun-Chao/coding-interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest31.java
55 lines (49 loc) · 1.48 KB
/
Test31.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Author: 王俊超
* Date: 2015-06-10
* Time: 19:54
* Declaration: All Rights Reserved !!!
*/
public class Test31 {
/**
* 题目2 输入一个整型数组,数组里有正数也有负数。数组中一个或连
* 续的多个整数组成一个子数组。求所有子数组的和的最大值。要求时间复杂度为O(n)。
*
* @param arr 输入数组
* @return 最大的连续子数组和
*/
public static int findGreatestSumOfSubArray(int[] arr) {
// 参数校验
if (arr == null || arr.length < 1) {
throw new IllegalArgumentException("Array must contain an element");
}
// 记录最大的子数组和,开始时是最小的整数
int max = Integer.MIN_VALUE;
// 当前的和
int curMax = 0;
// 数组遍历
for (int i : arr) {
// 如果当前和小于等于0,就重新设置当前和
if (curMax <= 0) {
curMax = i;
}
// 如果当前和大于0,累加当前和
else {
curMax += i;
}
// 更新记录到的最在的子数组和
if (max < curMax) {
max = curMax;
}
}
return max;
}
public static void main(String[] args) {
int[] data = {1, -2, 3, 10, -4, 7, 2, -5};
int[] data2 = {-2, -8, -1, -5, -9};
int[] data3 = {2, 8, 1, 5, 9};
System.out.println(findGreatestSumOfSubArray(data));
System.out.println(findGreatestSumOfSubArray(data2));
System.out.println(findGreatestSumOfSubArray(data3));
}
}