题解 | #牛牛的最大子数组和#

牛牛的最大子数组和

https://www.nowcoder.com/practice/a29134e2667948ea89c949dded92938b?tpId=363&tqId=10625878&ru=/exam/oj&qru=/ta/super-company23Year/question-ranking&sourceUrl=%2Fexam%2Foj

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param nums int整型一维数组
     * @return int整型
     */
    public int findMaxSubarraySum (int[] nums) {
        int tempMax = nums[0];
        int maxSum = nums[0];
        for (int i = 1; i < nums.length; i++) {
            tempMax = Math.max(nums[i], tempMax + nums[i]);
            maxSum = Math.max(maxSum, tempMax);
        }
        return maxSum;
    }
}

本题知识点分析:

1.动态规划

2.API函数(Math.max)

3.数学模拟

4.数组遍历

本题解题思路分析:

1.tempMax用于记录每一个遍历的数字时刻,临时的和是否大于0,用来判断正负数

2.maxSum用于记录连续最大的子数组和

本题使用编程语言: Java

如果你觉得本篇文章对你有帮助的话,可以点个赞,感谢!~

全部评论

相关推荐

牛客583549203号:腾讯还好,况且实习而已,实习生流动性很大,属于正常现象,记得和HR委婉解释
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务