大家注意,这个代码有bug,请用 {5,-7,-2,1} 测试。正确代码如下: public static int FindGreatestSumOfSubArray2(int[] array) { int total = array[0]; int max = array[0]; for(int i = 1; i<array.length; i++){ if(total < array[i] && total < 0){ total = array[i]; max = array[i]; }else { total += array[i]; if(total > max){ max = total; } } } return max; }
点赞

相关推荐

04-14 20:10
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务