lintcode 最小子数组

作者: yzawyx0220 | 来源:发表于2016-12-28 16:44 被阅读70次

给定一个整数数组,找到一个具有最小和的子数组。返回其最小和。
样例
给出数组[1, -1, -2, 1],返回 -3
解法跟思路与最大子数组完全一样,不多说了。
http://www.jianshu.com/p/46dd20963ee2

class Solution {
public:
    /**
     * @param nums: a list of integers
     * @return: A integer denote the sum of minimum subarray
     */
    int minSubArray(vector<int> nums) {
        // write your code here
        int sum = 0,result = nums[0];
        for (int i = 0;i < nums.size();i++) {
            sum += nums[i];
            result = min(result,sum);
            sum = min(sum,0);
        } 
        return result;
    }
};

相关文章

  • lintcode 最小子数组

    给定一个整数数组,找到一个具有最小和的子数组。返回其最小和。样例给出数组[1, -1, -2, 1],返回 -3解...

  • LintCode - 最小子数组(容易)

    版权声明:本文为博主原创文章,未经博主允许不得转载。 难度:容易 要求: 给定一个整数数组,找到一个具有最小和的子...

  • OJ lintcode 最小子数组

    给定一个整数数组,找到一个具有最小和的子数组。返回其最小和。注意事项子数组最少包含一个数字您在真实的面试中是否遇到...

  • 数组利用前缀和求子数组问题

    1、子数组之和https://www.lintcode.com/problem/subarray-sum/desc...

  • lintcode 数组划分

    给出一个整数数组 nums 和一个整数 k。划分数组(即移动数组 nums 中的元素),使得:所有小于k的元素移到...

  • LintCode连续数组

    这个题目的名字翻译的不好,题意是: 给一个二进制数组,找到 0 和 1 数量相等的子数组的最大长度样例样例 1:输...

  • 最大子数组

    最大子数组(lintcode 41) 描述:给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。样例:给出...

  • lintcode 最小子串覆盖

    给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串。注...

  • lintcode 最大子数组||

    给定一个整数数组,找出两个 不重叠 子数组使得它们的和最大。每个子数组的数字在数组中的位置应该是连续的。返回最大的...

  • lintcode 最大子数组|||

    给定一个整数数组和一个整数 k,找出 k 个不重叠子数组使得它们的和最大。每个子数组的数字在数组中的位置应该是连续...

网友评论

    本文标题:lintcode 最小子数组

    本文链接:https://www.haomeiwen.com/subject/rewyvttx.html