题目描述

请计算给出的数组(至少含有一个数字)中具有最大和的子数组(子数组要求在原数组中连续)
例如:给出的数组为[−2,1,−3,4,−1,2,1,−5,4],
子数组[−2,1,−3,4,−1,2,1,−5,4],具有最大的和:6.
拓展:
如果你已经提出了O(n)的解决方法,请尝试使用分治算法来解决这道题。这道题分治的解法更巧妙一些。

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.

More practice:

If you have figured out the O(n) solution, try coding another
solution using the divide and conquer approach, which is more subtle.

示例1

输入

复制

[1]

输出

复制

1
示例2

输入

复制

[-2,1,-3,4,-1,2,1,-5,4]

输出

复制

6
class Solution {
public:
    /**
     *
     * @param A int整型一维数组
     * @param n int A数组长度
     * @return int整型
     */
    int maxSubArray(int* A, int n) {
        // write code here
        int sum=A[0],maxSum=A[0];
        for (int i=1;i<n;i++){
            if (sum<0)
                sum=0;
            sum+=A[i];
            maxSum=max(maxSum,sum);
        }
        return maxSum;
    }
};

leetcode97:maximum -subarray的更多相关文章

  1. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  2. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  3. 算法:寻找maximum subarray

    <算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...

  4. LEETCODE —— Maximum Subarray [一维DP]

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  5. 【leetcode】Maximum Subarray

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  6. maximum subarray problem

    In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...

  7. (转)Maximum subarray problem--Kadane’s Algorithm

    转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...

  8. 3月7日 Maximum Subarray

    间隔2天,继续开始写LeetCodeOj. 原题: Maximum Subarray 其实这题很早就看了,也知道怎么做,在<编程珠玑>中有提到,求最大连续子序列,其实只需要O(n)的复杂度 ...

  9. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

随机推荐

  1. C/C++ 条件编译

    条件编译就是指有条件的编译,即根据条件去编译代码,在编译阶段时就对代码做出取舍,有的编译,有的不编译,这样比写成一个个判断函数更有效率,比如工程代码大部分的地方都类似,只有个别语句因为使用的硬件版本不 ...

  2. 优质分享 | Spring Boot 入门到放弃!!!

    持续原创输出,点击上方蓝字关注我 目录 前言 视频目录 如何获取? 总结 前言 最近不知不觉写Spring Boot专栏已经写了九篇文章了,从最底层的项目搭建到源码解析以及高级整合的部分,作者一直在精 ...

  3. 【Excel技巧】用IF函数进行等级评定

    如果下面给出一份"2月份语文成绩考核表",那么如何对成绩进行等级评定呢. 等级评定规则: 总分(100分) A级(91-100) B级(81-90) C级(71-80) D级(70 ...

  4. RHSA-2018:1200-重要: patch 安全更新(代码执行)

    [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) 修复命令: 使用root账号登陆She ...

  5. devops-jenkins基于角色的权限管理RBAC

    一. devops-jenkins基于角色的权限管理RBAC 1 安装角色的rbac角色管理  1.1) 点击系统管理 1.2) 选择插件管理 1.3) 选择可选插件,输入role搜索 1.4) 选择 ...

  6. redis 开启AOF

    找到redis 安装目录 例如 cd /usr/local/redis 打开 redis.conf  修改以下参数: # vi /usr/local/redis/etc/redis.conf appe ...

  7. ScanTailor-ScanTailor 强大的多方位的满足处理扫描图片的需求

    ScanTailor 强大的多方位的满足处理扫描图片的需求   ScanTailor 能做什么? 批量或单张或选择区间旋转图片 自动切割页面,同时提供手动选项 自动识别图像歪斜角度,同时提供手动选项 ...

  8. go cap和len区别

    首先要搞清楚容量和长度的区别: 容量是指底层数组的大小,长度指可以使用的大小 容量的用处在哪?在与当你用 appen d扩展长度时,如果新的长度小于容量,不会更换底层数组,否则,go 会新申请一个底层 ...

  9. centos8使用timedatectl管理时间

    一,centos8中默认使用chronyd来做时间服务 1,查看chronyd服务的状态 [root@blog ~]# systemctl status chronyd ● chronyd.servi ...

  10. sql分页 一条语句搞定

    select top 每页条数 * from ( SELECT ROW_NUMBER() OVER (ORDER BY id desc) AS RowNumber,* FROM Article  条件 ...