题解 | #牛群能量的最大连续和#
牛群能量的最大连续和
https://www.nowcoder.com/practice/6ce78bf3ec2040b5a35d123c690bb67a
import java.util.*;
//可以用动态规划,以下是简易版
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param energy int整型一维数组
* @return int整型
*/
public int maxEnergySum (int[] energy) {
// write code here
int n=energy.length;
int temp=energy[0];
int res=Integer.MIN_VALUE;
for(int i=1;i<n;i++){
temp=Math.max(temp+energy[i],energy[i]);
res=Math.max(res,temp);
}
return res;
}
}
查看9道真题和解析