题目描述

请计算给出的数组(至少含有一个数字)中具有最大和的子数组(子数组要求在原数组中连续)
例如:给出的数组为[−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. P3545 [POI2012]HUR-Warehouse Store

    题目描述 n天.第i天上午会进货Ai件商品,中午的时候会有顾客需要购买Bi件商品,可以选择满足顾客的要求,或是无视掉他. 如果要满足顾客的需求,就必须要有足够的库存.问最多能够满足多少个顾客的需求. ...

  2. 配置adpate的方式

    配置adpate的方式 资源文件配置 ArryAdapter<CharSequence>cadapt= ArryAdapter.createFromResource(this,资源id,a ...

  3. mycat的privileges标签

    参考https://blog.csdn.net/tornadojava/article/details/54948662 privileges标签 对用户的 schema以及表进行精细化的DML权限控 ...

  4. select函数详解(转)

    Select函数在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如connect. accept.recv或recvfrom这样的阻塞 ...

  5. 多测师讲解自动化测试 _RF模拟鼠标悬停_高级讲师肖sir

    test19 Open Browser https://www.ctrip.com/?sid=155952&allianceid=4897&ouid=index gc Maximize ...

  6. java基础语句翻译

    public static void main(String[] args) { System.out.println("人生中的第一个代码-----"); } } package ...

  7. 一入Java深似海

    Java的基础语法 一个java程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来写协同工作.下面简要介绍下类.对象.方法和实例变量的概念. 对象: 对象是一个类的实例,有状态和行为.例如 ...

  8. go http爬虫

    1 package main import ( "fmt" "io/ioutil" "net/http" ) func main() { r ...

  9. linux mkfifo命令基本用法

    首先了解linux命令执行顺序 通常情况下,终端只能执行一条命令,然后按下回车,那么执行多条命令呢 顺序执行多条命令,可以用分号; cmd1;cmd2;cmd3 条件执行多条命令,使用&&am ...

  10. 图的全部实现(邻接矩阵 邻接表 BFS DFS 最小生成树 最短路径等)

    1 /** 2 * C: Dijkstra算法获取最短路径(邻接矩阵) 3 * 6 */ 7 8 #include <stdio.h> 9 #include <stdlib.h> ...