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 the largest sum = 6.

Follow up:

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


方法一:不过这个方法对全是负数时,不成立;



class Solution {
public:
int maxSubArray(vector<int>& nums) {
int ans=nums[],sum=;
for(int i=; i<nums.size();i++){
sum+=nums[i];
ans=max(ans,sum);
sum=max(sum,);
}
return ans;
}
}; //Idea is very simple. Basically, keep adding each integer to the sequence until the sum drops below 0.
//If sum is negative, then should reset the sequence.

53. [LeetCode] Maximum Subarray的更多相关文章

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

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

  2. LeetCode: Maximum Subarray 解题报告

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

  3. [LeetCode]Maximum Subarray题解

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

  4. [LeetCode] Maximum Subarray Sum

    Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...

  5. leetcode || 53、Maximum Subarray

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

  6. [LeetCode]题53:Maximum Subarray

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  7. (LeetCode 53)Maximum Subarray

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

  8. LeetCode(53) Maximum Subarray

    题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...

  9. 【LeetCode算法-53】Maximum Subarray

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

随机推荐

  1. ie浏览器报 promise 问题

    1.首先安装:babel-polyfill    npm install babel-polyfill --save2.然后引入:babel-polyfill 在build目录下,webpack.ba ...

  2. activemq的高级特性:消息持久订阅

    activemq的高级特性之消息持久订阅 如果采用topic模式发送的时候,mq关闭了或消费者关闭了.在启动的时候,就会收不到mq发送的消息,所以就会出现消息持久订阅. 消息持久订阅:第一:消息要持久 ...

  3. 16JavaScript for循环

    1.JavaScript 循环 如果希望一遍又一遍地运行相同的代码,并且每次的值都不同,那么使用循环是很方便的. 我们可以这样输出数组的值: 一般写法: document.write(cars[0] ...

  4. node创建服务器

    //引入核心模块 const http = require('http'); //创建服务器 http.createServer((req,res)=>{ }).listen(3000); // ...

  5. css之层叠上下文和层叠顺序

    大家在写网页的时候会不会经常遇到莫名奇妙的样式问题,比如谁覆盖了谁.也找不出原因,为什么z-index高的却没有覆盖掉z-index低的元素呢? 带着这些疑问.我做了个小实验.代码如下: <st ...

  6. 03.搭建Spark集群(CentOS7+Spark2.1.1+Hadoop2.8.0)

    接上一篇:https://www.cnblogs.com/yjm0330/p/10077076.html 一.下载安装scala 1.官网下载 2.spar01和02都建立/opt/scala目录,解 ...

  7. 'express'不是内部或外部命令, 也不是可运行的程序, 或批处理文件

    1. npm install -g express-generator 安装新的express框架2. express -h 错误提示: 'express'不是内部或外部命令, 也不是可运行的程序, ...

  8. AC自动机(简单版)(施工ing)

    声明 想看加强版的戳这里(施工ing,作者正努力中)~ 先贴题目吧哎~   AC自动机加强版  洛谷 P3796 题目: 洛谷 P3808 (数据范围困了我好久 TAT) 反正涉及字符串的算法都很玄学 ...

  9. 20155204 《Java程序设计》实验一(Java开发环境的熟悉)实验报告

    实验一 Java开发环境的熟悉 一.实验内容及步骤 1.使用JDK编译.运行简单的java程序 步骤一:在linux界面下运行终端 步骤二:在终端中打开待编译文件的文件夹 步骤三:使用 javac 文 ...

  10. 20155217 《Java程序设计》第三次实验报告

    20155217 <Java程序设计>第三次实验报告 实验内容 XP基础 XP核心实践 相关工具 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)>&l ...