/** @return 两个元素的数组,表示结果序列的起始下标
*/ public int[] getMaxLenSum0(int[] a) {
int[] res = new int[2];
int maxLen = 0;
int right = 0;
for (int i = 0; i < a.length; i++) {
right = i + maxLen;//下一次检测时,右边界开始位置!!!!要点
while (right < a.length) {
while (right < a.length && !equalO(a, i, right)) {
right++;
}
if (right == a.length) {
break;
}
if (equalO(a, i, right)) {
maxLen = right - i;
if (res[1] - res[0] < maxLen) {
res[0] = i;
res[1] = right;
}
right++;
}
}
}
return res;
}
/**des:数组a 中left到right中的元素和是否为0
*/
private boolean equalO(int[] a, int left, int right) {
int sum = 0;
for (int i = left; i <= right; i++) {
sum += a[i];
}
return sum == 0;
}
应该还有改进的方式。比如验证是否和为0这个地方。