public static void main(String[] args) {
int[] num = { 1, 1, 1, 1, -4, -4, -4, -3, 4, 8, 6 };
int maxSum = 0;//定义最大数值
int thisSum = 0;//定义获取相加的数值
int begin = 0, start;//定义获得开始的索引
start = begin;
int end, finash = 0;//定义获得结束的索引
end = finash;
for (int i = 0; i < num.length; i++) {
finash = i;
thisSum += num[i];
if (thisSum > maxSum) {
maxSum = thisSum;
end = finash;//记录相加之后的索引位置
start = begin;
}
if (thisSum < 0) {
thisSum = 0;
begin = i + 1;
}
}
System.out.println(maxSum);
System.out.print("开始索引为"+start+"结束索引"+end);
}
JAVA最长子序列
最新推荐文章于 2024-07-27 17:56:15 发布