【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】

【053-Maximum Subarray(最大子数组和)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】

原题

  Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

  For example, given the array [?2,1,?3,4,?1,2,1,?5,4],

the contiguous subarray [4,?1,2,1] has the largest sum = 6.

题目大意

  求数组的最大子数组的和。

解题思路

  动态规划问题,已知了前k个元素的最大子序列和为maxSub(已经被记录下来了),以及一个临时和sum,如果添加了第k+1这个元素,由于是连续子序列这个限制,所以如果k+1这个元素之前的和是小于0的,那么对于增大k+1这个元素从而去组成最大子序列是没有贡献的,所以可以把sum 置0。

代码实现

算法实现类

public class Solution {

    public int maxSubArray(int[] nums) {
        // 参数校验
        if (nums == null || nums.length < 1) {
            throw new IllegalArgumentException();
        }

        int max = Integer.MIN_VALUE;
        int curSum = 0;

        for (int i : nums) {
            // 当前和小于0,就将当前值赋给curSum
            if (curSum <= 0){
                curSum = i;
            }
            // 否则进行累加
            else {
                curSum += i;
            }

            // 保存较大的值
            if (max < curSum) {
                max = curSum;
            }
        }

        return max;
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47120487

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 19:04:57

【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】的相关文章

lintcode 中等题:maximum subarray最大子数组II

题目 最大子数组 II 给定一个整数数组,找出两个不重叠子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 您在真实的面试中是否遇到过这个题? Yes 样例 给出数组[1, 3, -1, 2, -1, 2],这两个子数组分别为[1, 3]和[2, -1, 2]或者[1, 3, -1, 2]和[2],它们的最大和都是7 注意 子数组最少包含一个数 挑战 要求时间复杂度为O(n) 解题 最大子数组I 这个题目是求一个数组中一个最大子数组的和,而本题目是求数组中的前

LeetCode 70 _ Maximum Subarray 最大子数组 (Easy)

Description: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Given n will be a positive integer. Example 1: Input: 2 Output: 2

[LeetCode] Maximum Subarray 最大子数组

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6. click to show more practice. Mor

【LeetCode每天一题】Maximum Subarray(最大子数组)

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example:        Input: [-2,1,-3,4,-1,2,1,-5,4],            Output: 6                 Explanation: [4,-1,2,1] has

Maximum Subarray (最大子数组)

题目描述: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. 这道题很经典,<算法导论>上有相关讨论,但是没有

53. Maximum Subarray最大子数组

[抄题]: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the largest sum = 6. 方法一:贪心算法greedy [一句话思路]: 每次

[LintCode] Maximum Subarray 最大子数组

Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarray should contain at least one number. Have you met this question in a real interview? Yes Example Given the array [−2,2,−3,4,−1,2,1,−5,3], the contigu

【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】

[121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one

【LeetCode-面试算法经典-Java实现】【223-Rectangle Area(矩形区域)】

[223-Rectangle Area(矩形区域)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner

【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】

[104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题目大意 给定