/**
* 最大子数组的暴力求解算法,复杂度为o(n2)
* @param n
* @return
*/
static MaxSubarray findMaxSubarraySlower(int[] n) {
long tempSum = 0;
int left = 0;
int right = 0;
long sum = Long.MIN_VALUE;
for (int i = 0; i < n.length; i++) {
for (int j = i; j < n.length; j++) {
tempSum += n[j];
if (tempSum > sum) {
left = i;
right = j;
sum = tempSum;
}
}
tempSum = 0;
}
return new MaxSubarray(left, right, sum);
}
转载于:https://my.oschina.net/xiaojintao/blog/817353
本文介绍了一种复杂度为O(n^2)的最大子数组暴力求解算法,通过双重循环遍历数组,计算所有可能子数组的和,找出最大和对应的子数组。
2013

被折叠的 条评论
为什么被折叠?



