/**
* 最大子数组的暴力求解算法,复杂度为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);
}
第四章 分治策略 4.1 最大子数组问题 (暴力求解算法),布布扣,bubuko.com
时间: 2024-10-11 22:56:13