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. vue中使用swiper并自定义分页器样式

    一,安装swiper 执行命令 npm install vue-awesome-swiper --save 二,引入swiper import {Swiper} from "vue-awes ...

  2. Ubuntu下apt方式安装与更新Git

    本人使用的系统 Ubuntu 18.04.1 ,使用apt安装Git: sudo apt insatll git 安装后发现不是最新的版本,更新方法: sudo add-apt-repository ...

  3. Docker学习系列(一)-CentOS7下安装Docker

    CentOS7下Docker的安装 一.操作系统要求 CentOS 7 64位 Kernel 3.10+ 本机系统信息 二.卸载旧版本 如果之前安排过旧版本的Docker,先卸载掉旧版Docker以及 ...

  4. eclipse 配置

    访问地址        https://www.eclipse.org/ . . . 配置工作目录:存放1.项目代码    2.IDE相关配置信息 //修改编码时的字体 //修改编码格式 没有配置to ...

  5. jQuery之修改li下样式和图片

    <script type="text/javascript"> $(document).ready(function(){ $('li').click(function ...

  6. 基于 HTML5 Canvas 的 3D 渲染引擎构建机架式服务器

    前言 今天找到了 HT 的官网里的 Demo 网站( http://www.hightopo.com/demos/index.html ),看的我眼花缭乱,目不暇接. 而且 HT 的用户手册,将例子和 ...

  7. redis迁移复制数据,主从关系建立实践

    装redis的机器出了点问题,需要转移数据然后初始化系统,然后我就研究了下redis的数据复制,发现了slaveof 192.168.0.1 6379这个命令,开始踩下这个坑 首先要新的服务器上进入r ...

  8. vue-nuxt.js部署到宝塔主机服务器

    废话不多说,直接上步骤,如下: 本文章为在 vue环境下使用了nuxt.js 1.搭建环境--由于本人安装的是宝塔主机,因此如下: 由于我直接使用的是宝塔主机,直接去“软件管理”安装 PM2管理器. ...

  9. iOS 基于APNS消息推送原理与实现(包括JAVA后台代码)

    Push的原理: Push 的工作机制可以简单的概括为下图   图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple ...

  10. 了解MapReduce_2

    再写MapReduce执行流程之前,首先先对MapReduce有一些了解: 1. 简介 MapReduce是一个计算框架,既然是做计算的框架,那么表现的形式上就有输入,操作输入,得到结果2.主从结构 ...