题解 | #小猪摘水果#
小猪摘水果
https://www.nowcoder.com/practice/fdb76b9170dc4e689a7eceee97159d96
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param fruit int整型一维数组
* @return int整型
*/
public int mostFruitTree (int[] fruit) {
// write code here
int sum = 10;
int max = 10;
for (int i = 0; i < fruit.length; i++) {
sum += fruit[i];
max = Math.max(sum, max);
}
return max;
}
}
知识点:
1.数组遍历
2.遍历同时求最大和
3.数学模拟
解题思路:
1.遍历每个fruit
2.每次去比较sum和max,取最大值就可以
3.最后返回max
查看17道真题和解析