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.

class Solution {
public int maxSubArray(int[] nums) {
if(nums==null||nums.length==0) return 0; int maxSum=nums[0];//最大和。初始值要为第一项
int theSum=nums[0];//当前项为止的最大和。初始值要为第一项,不要为0
        //如果当前项的最大和小于等于0,那么theSum应该要从该项后面开始重新计算和,因为负数对和没有正帮助。
for(int i=1;i<nums.length;i++){
if(theSum>0)
theSum=theSum+nums[i];
else
theSum=nums[i];//初始值不为零,则这里初始化时也就不是0,
if(maxSum<theSum)
maxSum=theSum;
} return maxSum;
}
}

初始为0,若只有一个元素,且为负数,此时就会输出0.不对

Maximum Subarray(最大子数组)的更多相关文章

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

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

  2. [LeetCode] 53. Maximum Subarray 最大子数组

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

  3. [leetcode]53. Maximum Subarray最大子数组和

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

  4. [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治

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

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

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

  6. 【LeetCode每天一题】Maximum Subarray(最大子数组)

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

  7. [Leetcode] maximun subarray 最大子数组

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

  8. 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...

  9. 53. Maximum Subarray最大子序和

    网址:https://leetcode.com/problems/maximum-subarray/submissions/ 很简单的动态规划 我们可以把 dp[i] 表示为index为 i 的位置上 ...

随机推荐

  1. Android触摸屏幕时间-android学习之旅(三)

    android的多点触摸是经常遇到的编程技巧,这一篇可以将详细的介绍这个问题. 简单实例 android的触摸需要实现OnTouchListener接口,继承里面方法. 布局代码: <?xml ...

  2. Sharepoint Solution Gallery Active Solution时激活按钮灰色不可用的解决方法

    在做CRM与sharepoint集成的时候,需要在sharepoint中上传crmlistcomponent组件,上传后需要激活,但会碰到激活按钮是灰色的无法点击的问题,如下图中这样,包括点击组件后面 ...

  3. 【一天一道LeetCode】#141. Linked List Cycle

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【java虚拟机系列】java中类与对象的加载顺序

    首先了解一下Java虚拟机初始化的原理. JVM通过加装.连接和初始化一个Java类型,使该类型可以被正在运行的Java程序所使用.类型的生命周期如下图所示: 装载和连接必须在初始化之前就要完成. 类 ...

  5. iOS 解决键盘挡住输入框的问题

    iOS开发中经常会用到输入框UITextField,所以也常会遇到键盘挡住输入框而看不到输入框的内容. 在这里记录一种方法,用UITextField的代理来实现View的上移来解决这个问题. 首先设置 ...

  6. python类:描述器Descriptors和元类MetaClasses

    http://blog.csdn.net/pipisorry/article/details/50444769 描述器(Descriptors) 描述器决定了对象属性是如何被访问的.描述器的作用是定制 ...

  7. MySQL慢查询优化 EXPLAIN详解

            我们平台过一段时间就会把生产数据库的慢查询导出来分析,要嘛修改写法,要嘛新增索引.以下是一些笔记.总结整理 慢查询排查         show status;  // 查询mysql ...

  8. EFI怎么装系统? UEFI BIOS

    关于EFI的介绍,就不赘述了. 大家可以看看这个帖子 http://benyouhui.it168.com/thread-2488583-1-1.html 总之,新电脑都是这玩意,win8也做了相应E ...

  9. ROS_Kinetic_05 ROS基础内容(二)

    ROS_Kinetic_05 ROS基础内容(二) 1. ROS节点node 官网教程:http://wiki.ros.org/cn/ROS/Tutorials/UnderstandingNodes ...

  10. 看看腾讯是怎么做产品设计分析的 - 腾讯QQ音乐业务产品规划