问题:
在给定的数组中寻找“和最大”的那个子数组
来源: http://topic.youkuaiyun.com/u/20080507/13/86742668-0c78-404d-8bf0-d7bd515dbd9b.html
这是我的代码,测试了几下没有发现问题,但是还不知道正确否。
待大家检验。发现问题要告诉我啊!
package
niko7.csdn;

import
java.util.ArrayList;
import
java.util.Arrays;
import
java.util.Iterator;
import
java.util.LinkedList;

/**
* 在给定的数组中寻找“和最大”的那个子数组
*
* 转载、引用必须保留此段信息。
* @author niko7,(顾法华,杭州)
* @email niko7@163.com
*
*/
public
class
MaxSubArray
{
public static void main(String[] args)
{
ArrayList<int[]> testData = new ArrayList<int[]>();
//这是一些测试用的数组
testData.add(new int[]{-7,15,2,-5,12,6,-26});
testData.add(new int[]{1});
testData.add(new int[]{12, -8, 5, 66, -21, 0,35, -44,7});
testData.add(new int[]{-100,-100,-100,-100,100});
testData.add(new int[]{3,73,-95,42,43,29,-30,-87,74,-53,22,74,-91,-1,-27,-8,-14,26,-67,-74});
testData.add(new int[]{12, -8, 71, -21, 35, -44,7 ,-2000,1999});
testData.add(new int[]{98,99,-2,-99,100,-99,-2});
testData.add(new int[]{-2, 11, -4, 13, -5, -2});
testData.add(new int[]{-1});
for(int[] array:testData)
{
System.out.println(Arrays.toString(array));
System.out.println("result:"+findMaxSubArray(array));
System.out.println();
}
}
static private SubArray findMaxSubArray(int[] intArray)
{
//去头,去尾
int bIndex = 0,eIndex = intArray.length-1;
while(intArray[bIndex]<=0 && bIndex+1<intArray.length){bIndex++;}
while(intArray[eIndex]<=0 && eIndex-1>=0){eIndex--;}
LinkedList<SubArray> saList = new LinkedList<SubArray>();
for(int i=bIndex; i<=eIndex; i++)
{
saList.add(new SubArray(i,intArray[i]));
}

//合并相邻的属性相似的数据
Iterator<SubArray> iter = saList.iterator();
if(iter.hasNext())
{
SubArray a = iter.next();
while(iter.hasNext())
{
SubArray b = iter.next();
if((a.getSum()>=0 && b.getSum()>=0)||(a.getSum()<=0 && b.getSum()<=0))
{
a.m(b);
iter.remove();
}
else
{
a = b;
}
}
}
//跨区段合并更大范围的数据
SubArray[] array = new SubArray[saList.size()];
array = saList.toArray(array);
if(array.length>=3)
{
for(int i=1;i<array.length; i+=2)
{
SubArray a = array[i-1];
SubArray b = array[i];
SubArray c = array[i+1];
int sum = a.getSum()+b.getSum()+c.getSum();
if( sum > a.getSum() && sum > c.getSum())
{
a.m(b);
a.m(c);
array[i-1] = null;
array[i] = null;
array[i+1] = a;
}
}
}
//找到sum值最大的一段
SubArray maxSubArray = null;
for(int i=0;i<array.length; i++)
{
if(null!=array[i])
{
if(null==maxSubArray || array[i].getSum()>maxSubArray.getSum())
{
maxSubArray = array[i];
}
}
}
return maxSubArray;
}
/**
* 为了记录原始下标而建立的 子数组 对象
* @author 顾法华,杭州
*
*/
static private class SubArray
{
int head = -1;
LinkedList<Integer> content;
SubArray(int head,LinkedList<Integer> subArray)
{
this.head = head;
this.content = subArray;
}
SubArray(int head,int v)
{
this(head,new LinkedList<Integer>());
content.add(v);
}
public void m(SubArray sa)
{
content.addAll(sa.content);
}
public int getSum()
{
int s = 0;
for(int i : content)
{
s += i;
}
return s;
}
public String toString()
{
return this.getSum()+" ("+head+"~"+(head+content.size()-1)+") "+content.toString();
}
}
}
下面是跑出来的结果:
[
-
7
,
15
,
2
,
-
5
,
12
,
6
,
-
26
]
result:
30
(
1
~
5
) [
15
,
2
,
-
5
,
12
,
6
]

[
1
]
result:
1
(
0
~
0
) [
1
]

[
12
,
-
8
,
5
,
66
,
-
21
,
0
,
35
,
-
44
,
7
]
result:
89
(
0
~
6
) [
12
,
-
8
,
5
,
66
,
-
21
,
0
,
35
]

[
-
100
,
-
100
,
-
100
,
-
100
,
100
]
result:
100
(
4
~
4
) [
100
]

[
3
,
73
,
-
95
,
42
,
43
,
29
,
-
30
,
-
87
,
74
,
-
53
,
22
,
74
,
-
91
,
-
1
,
-
27
,
-
8
,
-
14
,
26
,
-
67
,
-
74
]
result:
117
(
8
~
11
) [
74
,
-
53
,
22
,
74
]

[
12
,
-
8
,
71
,
-
21
,
35
,
-
44
,
7
,
-
2000
,
1999
]
result:
1999
(
8
~
8
) [
1999
]

[
98
,
99
,
-
2
,
-
99
,
100
,
-
99
,
-
2
]
result:
197
(
0
~
1
) [
98
,
99
]

[
-
2
,
11
,
-
4
,
13
,
-
5
,
-
2
]
result:
20
(
1
~
3
) [
11
,
-
4
,
13
]

[
-
1
]
result:
-
1
(
0
~
0
) [
-
1
]

在给定的数组中寻找“和最大”的那个子数组
来源: http://topic.youkuaiyun.com/u/20080507/13/86742668-0c78-404d-8bf0-d7bd515dbd9b.html
这是我的代码,测试了几下没有发现问题,但是还不知道正确否。
待大家检验。发现问题要告诉我啊!
























































































































































下面是跑出来的结果:



























